Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create multiple folders if it does not exist using python

Tags:

python

I have several xml files(more than 20000) present in one directory. I need to get the application folder name from each xml file then move the file to its respective application folder (application name can be same for two files).

With "if not os.path.exists('Working_Dir/app'):" I am trying to check for each application if it is present or not. With next line I am trying to create that folder but somehow it is not checking for folder existence.

 #!/usr/bin/python

import os, sys, glob

Working_Dir = "/home"
path1 = "/home/xmlFiles"
path2 = "/home/JobFolder"

if not os.path.exists(path1):
    os.mkdir(path1, 0755);

if not os.path.exists(path2):
    os.mkdir(path2, 0755);

for files in glob.glob("*.xml"):
    f = open( files)
    for line in f:
        if "ParentApplication" in line:
            app = line.split('.')[1]
            if not os.path.exists('Working_Dir/app'):
                os.makedirs(app)

Below is the error which i am getting.

$ python test.py
Traceback (most recent call last):
  File "test.py", line 21, in <module>
    os.mkdir(app, 0755);
OSError: [Errno 17] File exists: 'TRC'
like image 399
ankitpandey Avatar asked Feb 16 '26 08:02

ankitpandey


1 Answers

I think this links maybe help you.

  1. python-2-6-file-exists-errno-17
  2. python-fileexists-error-when-making-directory

I think what you do is just to raise the exception.I can not reappear your Exception.

like image 71
chyoo CHENG Avatar answered Feb 18 '26 22:02

chyoo CHENG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!