我想遍歷現有路徑和文件名的文本文件中的每一行,將字符串分爲驅動器,路徑和文件名。那麼我想要做的就是將這些文件的路徑複製到一個新的位置 - 或者是一個不同的驅動器,或者追加到一個現有的文件樹中(即,如果S:\ A \ B \ C \ D \ E \ F.shp是我的原始文件,我希望將它添加到新的位置作爲C:\ users \ visc \ A \ B \ C \ D \ E \ F.shpPython os.makedirs重新創建路徑
由於我的編程能力差,我繼續收到錯誤:
File "C:\Users\visc\a\b.py", line 28, in <module>
(destination) = os.makedirs(pathname, 0755);
這裏是我的代碼:
進口操作系統,SYS,shutil
## Open the file with read only permit
f = open('C:/Users/visc/a/b/c.txt')
destination = ('C:/Users/visc')
# read line by line
for line in f:
line = line.replace("\\\\", "\\")
#split the drive and path using os.path.splitdrive
(drive, pathname) = os.path.splitdrive(line)
#split the path and fliename using os.path.split
(pathname, filename) = os.path.split(pathname)
#print the stripped line
print line.strip()
#print the drive, path, and filename info
print('Drive is %s Path is %s and file is %s' % (drive, pathname, filename))
(destination) = os.makedirs(pathname, 0755);
print "Path is Created"
謝謝
您沒有包含實際的錯誤信息。 –
部分追溯?更糟糕的是,根本沒有發佈。 – KurzedMetal
對不起,這裏是我的完整錯誤信息:'Traceback(最近調用最後一個): 文件「C:\ Users \ visc \ a \ b.py」,第28行,在 (destination)= os.makedirs路徑名,0755); mkdir(名稱,模式) WindowsError:[錯誤183]當該文件已存在時,無法創建文件:'\ C:\ Python26 \ ArcGIS10.0 \ lib \ os.py「 \ A \\ B \\ C \\ D \\ E'' –
Visceral