Please find my python script below:
import os;
import sys;
dir_dst = sys.argv[1]
for x in range(150) :
dirname = str(x)
dst_dir = os.path.join(dir_dst, dirname)
dirname = "annotation"
dst = os.path.join(dst_dir, dirname)
print dst
if not os.path.exists(dst_dir):
os.mkdir(dst)
The aim is to create a directory called "annotation" within each of the numbered directories ranging as in the code above. This code doesn't do it and on printing the value of "dst", here's an example of what it shows:
NLP/test data/reconcile/0\annotation
NLP/test data/reconcile/1\annotation
How can this be resolved?
Change the second to last line to
if not os.path.exists(dst):
Right now you're checking if the original directory exists.
Related
I have a parent directory that contains a lot of subdirectories. I want to create a script that loops through all of the subdirectories and removes any key words that I have specified in the list variable.
I am not entirely sure how to acheive this.
Currently I have this:
import os
directory = next(os.walk('.'))[1]
stringstoremove = ['string1','string2','string3','string4','string5']
for folders in directory:
os.rename
And maybe this type of logic to check to see if the string exists within the subdirectory name:
if any(words in inputstring for words in stringstoremove):
print ("TRUE")
else:
print ("FALSE")
Trying my best to to deconstruct the task, but I'm going round in circles now
Thanks guys
Startng from your existing code:
import os
directory = next(os.walk('.'))[1]
stringstoremove = ['string1','string2','string3','string4','string5']
for folder in directory :
new_folder = folder
for r in stringstoremove :
new_folder = new_folder.replace( r, '')
if folder != new_folder : # don't rename if it's the same
os.rename( folder, new_folder )
If you want to rename those sub directories which match in your stringstoremove list then following script will be helpful.
import os
import re
path = "./" # parent directory path
sub_dirs = os.listdir(path)
stringstoremove = ['string1','string2','string3','string4','string5']
for directory_name in sub_dirs:
if os.path.isdir(path + directory):
for string in stringstoremove:
if string in directory_name:
try:
new_name = re.sub(string, "", directory_name)
os.rename(path + directory, path + new_name) # rename this directory
except Exception as e:
print (e)
I would like to use the shutil.move() function to move some files which match a certain pattern to a newly created(inside python script)folder, but it seems that this function only works with existing folders.
For example, I have 'a.txt', 'b.txt', 'c.txt' in folder '/test', and I would like to create a folder '/test/b' in my python script using os.join() and move all .txt files to folder '/test/b'
import os
import shutil
import glob
files = glob.glob('./*.txt') #assume that we in '/test'
for f in files:
shutil.move(f, './b') #assume that './b' already exists
#the above code works as expected, but the following not:
import os
import shutil
import glob
new_dir = 'b'
parent_dir = './'
path = os.path.join(parent_dir, new_dir)
files = glob.glob('./*.txt')
for f in files:
shutil.move(f, path)
#After that, I got only 'b' in '/test', and 'cd b' gives:
#[Errno 20] Not a directory: 'b'
Any suggestion is appreciated!
the problem is that when you create the destination path variable name:
path = os.path.join(parent_dir, new_dir)
the path doesn't exist. So shutil.move works, but not like you're expecting, rather like a standard mv command: it moves each file to the parent directory with the name "b", overwriting each older file, leaving only the last one (very dangerous, because risk of data loss)
Create the directory first if it doesn't exist:
path = os.path.join(parent_dir, new_dir)
if not os.path.exists(path):
os.mkdir(path)
now shutil.move will create files when moving to b because b is a directory.
I have a parent folder, lets call it "workspace". Within this parent folder, there are sub folders which have further subfolders that have a specific naming convention. It looks something like this:
- Workspace
- Subfolder A
- Name
- Image
- Class
- Subfolder B
- Name
- Image
- Class
- Subfolder C
- Name
- Image
- Class
I need help of some sort or direction writing a script that iterates through A-C within the workspace and copying all files in the "images" folder of each subfolder to a new destination.
This is what I have so far:
import os
import arcpy
import shutil
import fnmatch
workspace = "source"
pfolder = "rootdir"
files = os.listdir(workspace)
print (files)
test = workspace + "\\scratch.gdb"
if os.path.exists(test):
print ("Scratch GDB already exists")
shutil.rmtree(test)
scratch = arcpy.CreateFileGDB_management(workspace,"scratch")
print ("Original Scratch GDB removed and new GDB created ")
else:
scratch = arcpy.CreateFileGDB_management(workspace,"scratch")
print ("Scratch GDB has been created")
def main():
for dirname, dirnames, filenames in os.walk(pfolder):
for file in filenames:
if fnmatch.fnmatch(file,"*.jpg")==True:
shutil.copy2(file,scratch)
print("Files have been copied!")
else:
print("Error in copying files")
I want to copy all jpg files in that subdirectory and place them in a geodatabase. For some reason it does not run the line of code that executes the loop and copy.
Shutil may not work, to input a raster file in a geodatabase you cannot use the file extension in the name.
The code below is your code with minimal modification (like using CopyRaster_management instead copy2) to work, so it may not be the best code because I was not worried about that, but works:
import os
import arcpy
import shutil
import fnmatch
workspace = "C:\\Teste\\"
pfolder = r'C:\Teste\\'
files = os.listdir(workspace)
print (files)
tests = workspace + "\\scratch.gdb"
sGdbP = "C:\\Teste\\scratch.gdb\\"
if os.path.exists(tests):
print ("Scratch GDB already exists")
shutil.rmtree(tests)
scratch = arcpy.CreateFileGDB_management(workspace,"scratch")
print ("Original Scratch GDB removed and new GDB created ")
else:
scratch = arcpy.CreateFileGDB_management(workspace,"scratch")
print ("Scratch GDB has been created")
for dirname, dirnames, filenames in os.walk(pfolder):
for file in filenames:
if fnmatch.fnmatch(file,"*.tif")==True:
try:
arcpy.env.workspace = dirname
in_data = file
out_data = sGdbP + file[:-4] # cannot use extension
arcpy.CopyRaster_management(in_data, out_data)
except:
print "Raster To Geodatabase example failed."
print arcpy.GetMessages()
print("Files have been copied!")
print "End of script"
I tried to make a program which delete all of the empty files ( whose size is zero ). Then, i run the program by dragging the script file in "command prompt" and run it .
However, no empty files had deleted (but i have some of them).
Please help me to find the error in my code.
import os
a = os.listdir('C:\\Python27')
for folder in a :
sizes = os.stat('C:\\Python27')
b = sizes.st_size
s = folder
if b == 0 :
remove('C:\\Python27\s')
You're assigning the values iterator os.listdir returns to folder and yet you aren't using it at all in os.stat or os.remove, but instead you are passing to them fixed values that you don't need.
You should do something like this:
import os
dir = 'C:\\Python27'
for file_name in os.listdir(dir):
file_path = os.path.join(dir, file_name)
if os.stat(file_path).st_size == 0:
os.remove(file_path)
You can delete something like the following code and you need to add some exception handling. I have used a test folder name to demonstrate.
import os
import sys
dir = 'c:/temp/testfolder'
for root, dirs, files in os.walk(dir):
for file in files:
fname = os.path.join(root, file)
try:
if os.path.getsize(fname) == 0:
print("Removing file %s" %(fname))
os.remove(fname)
except:
print("error: unable to remove 0 byte file")
raise
When I open a file, I have to specify the directory that it is in. Is there a way to specify using the current directory instead of writing out the path name? I'm using:
source = os.listdir("../mydirectory")
But the program will only work if it is placed in a directory called "mydirectory". I want the program to work in the directory it is in, no matter what the name is.
def copyfiles(servername):
source = os.listdir("../mydirectory") # directory where original configs are located
destination = '//' + servername + r'/c$/remotedir/' # destination server directory
for files in source:
if files.endswith("myfile.config"):
try:
os.makedirs(destination, exist_ok=True)
shutil.copy(files,destination)
except:
this is a pathlib version:
from pathlib import Path
HERE = Path(__file__).parent
source = list((HERE / "../mydirectory").iterdir())
if you prefer os.path:
import os.path
HERE = os.path.dirname(__file__)
source = os.listdir(os.path.join(HERE, "../mydirectory"))
note: this will often be different from the current working directory
os.getcwd() # or '.'
__file__ is the filename of your current python file. HERE is now the path of the directory where your python file lives.
'.' stands for the current directory.
try:
os.listdir('./')
or:
os.listdir(os.getcwd())