使用python创建多个不存在的文件夹
我在一个目录中有几个xml文件(超过20000个)。我需要从每个xml文件中获取应用程序文件夹名称,然后将该文件移动到其各自的应用程序文件夹(两个文件的应用程序名称可以相同) 使用使用python创建多个不存在的文件夹,python,Python,我在一个目录中有几个xml文件(超过20000个)。我需要从每个xml文件中获取应用程序文件夹名称,然后将该文件移动到其各自的应用程序文件夹(两个文件的应用程序名称可以相同) 使用“如果没有os.path.exists('Working_Dir/app'):”我试图检查每个应用程序是否存在。在下一行中,我试图创建那个文件夹,但不知何故,它并没有检查文件夹是否存在 #!/usr/bin/python import os, sys, glob Working_Dir = "/home" pat
“如果没有os.path.exists('Working_Dir/app'):”
我试图检查每个应用程序是否存在。在下一行中,我试图创建那个文件夹,但不知何故,它并没有检查文件夹是否存在
#!/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)
下面是我得到的错误
$ 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'
$python test.py
回溯(最近一次呼叫最后一次):
文件“test.py”,第21行,在
os.mkdir(app,0755);
OSError:[Errno 17]文件存在:“TRC”
我认为这些链接可能对您有所帮助
我认为您所做的只是引发异常。我无法重新显示您的异常。shutil.move('Working\u Dir/f.name','Working\u Dir/app')
是否正确?
我想它正试图在工作目录中搜索f.name
。
看看这是否有帮助:
import os, sys, glob, errno, shutil`
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("*.py"):
f = open( files)
for line in f:
if "test." in line:
app = line.split('.')[1]
print app
try:
print app
os.makedirs(Working_Dir+'/'+app, 0755)
except OSError as exception:
if exception.errno != 17:
raise
s=Working_Dir+'/'+f.name
t=Working_Dir+'/'+app+'/'
shutil.move(s, t)
谢谢,在将此错误保留为异常后,它正在工作。创建文件夹后,我尝试将文件移动到相应的文件夹,但这会引发错误。如果第行中的“ParentApplication”为:app=line.split('..)[1]请尝试:os.makedirs(app,0755)除了作为异常的操作错误:if exception.errno!=17:raise shutil.move('Working_Dir/f.name','Working_Dir/app')下面是错误:IOError:[Errno 2]没有这样的文件或目录:'Working_Dir/f.name'