10

def copy_excel():

 srcpath = "C:\\Aloha" #where the excel files are located srcfiles = os.listdir(srcpath) #sets srcfiles as list of file names in the source path folder destpath = "C:\\" #destination where the folders will be created destdir = list(set([filename[19:22] for filename in srcfiles])) #extract three digits from filename to use for folder creation (i.e 505, 508, 517,...) #function to handle folder creation def create(dirname, destpath): full_path = os.path.join(destpath, dirname) if os.path.exists(full_path): pass else: os.mkdir(full_path) return full_path #function to handle moving files to appropriate folders def move(filename, dirpath): shutil.move(os.path.join(srcpath, filename), dirpath) #creates the folders with three digits as folder name by calling the create function above targets = [(folder, create(folder, destpath)) for folder in destdir] #handles moving files to appropriate folders if the three digits in file name matches the created folder for dirname, full_path in targets: for filename in srcfiles: if dirname == filename[19:22]: move(filename, full_path) else: pass 

I am somewhat new to Python so please bear with me! I was able to find this code block on this site and tailored it to my specific use case. The code works well for creating the specified folders and dropping the files into the corresponding folders. However, when I run the code again for new files that are dropped into the "C:\\Aloha" I get a Error 183: Cannot create a file when that file already exists. In this case, the folder already exists because it was previously created when the script was run the first time.

The code errors out when targets attempts to create folders that already exist. My question is, what is the logic to handle folders that already exists and to ignore the error and just move the files to the corresponding folders? The script should only create folders if they are not already there.

Any help would be greatly appreciated! I have attempted try/except and nesting if/else statements as well as os.path.isdir(path) to check to see if the directory exists but I haven't had any luck. I apologize for any of the comments that are wrong, I am still learning Python logic as I build this script out.

1
  • os.path.exists to check and use a try/except block to continue if error raised. Commented Jan 31, 2017 at 14:21

3 Answers 3

31

You could use os.makedirs which not only will create cascading directories such that for instance C:\foo\bar\qux will create foo, bar and qux in case they do not exist, but you can also set the exist_ok=True such that no error is thrown if the directory exists.

So:

os.makedirs(full_path,exist_ok=True) 
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the reply! Is this the same is putting if os.path.exists(full_path): pass? I am running Python 2.7.8 (I know, older version) which means I can't use the exist_ok=True parameter. I got the code to work for my purposes but ended up having to use Derlin's response
not pass: if not os.path.exists(full_path) : os.mkdir(full_path) (with not) and actually no. It is still not fully equivalent because of the cascade. Furthermore actually I would not consider python-3.x a real successor of python-2.7: both languages differ on so much aspects that they are - by some - considered different languages.
Version compatibility note: exist_ok was added in Python 3.2
3

In case you want to throw an error or stop the processing if the directory exists, you can use os.path.exists(full_path) before mkdir.

1 Comment

os.path.exist(full_path) forces to overwrite previous folders with the same name
1

Another option I just came by...

try: os.mkdir(path) except FileExistsError: pass 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.