【发布时间】:2015-09-12 20:11:01
【问题描述】:
我在一个目录中有多个 xml 文件(超过 20000 个)。我需要从每个 xml 文件中获取应用程序文件夹名称,然后将文件移动到其各自的应用程序文件夹(两个文件的应用程序名称可以相同)。
对于"if not 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