这是我开始的所有代码:
import shutil
import glob
import os
for filename in glob.glob(r"C:/Users/Aydan/Desktop/RTHPython/Years/*.txt"):
text_file_name = filename.strip()
dst = (os.path.join(r"C:/Users/Aydan/Desktop/SortedImages", text_file_name))
with open (text_file_name) as my_file:
for filename in my_file:
file_name = filename.strip()
src = (os.path.join('C:/Users/Aydan/Desktop', file_name))
src = src.replace('/', '\\')
shutil.move(src, dst)
我已经让它循环遍历\Years\
中的每个文本文件,并在每个文本文件中循环显示每一行。在文本文件的每一行上都有一个文件目录:
1855.txt contents:
/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0001_1.txt
/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0002_1.txt
/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0003_1.txt
1856.txt contents:
/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0004_1.txt
/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0005_1.txt
/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0006_1.txt
此文件目录用作使用shutil的文件源,目标是没有.txt的文本文件的名称,因此1855和1856.这意味着1855年以下的文件进入1855文件夹,和1856年一样。
我仍然在使用此代码时遇到错误,但我不明白为什么:
Traceback (most recent call last):
File "C:\Users\Aydan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 544, in move
os.rename(src, real_dst)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:/Users/Aydan/Desktop/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0001_1.txt' -> 'C:\\Users\\Aydan\\Desktop\\RTHPython\\Years\\1855.txt'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#17>", line 8, in <module>
shutil.move(src, dst)
File "C:\Users\Aydan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 558, in move
copy_function(src, real_dst)
File "C:\Users\Aydan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 257, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\Aydan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 120, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/Aydan/Desktop/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0001_1.txt'
>>>
即使它说No such file or directory:
,我知道它就在那里,因为当我在确切目录中粘贴它有错误时,文本文件会打开...
我非常感谢帮助解决这个问题并且几乎完成了这个项目:D
谢谢!
Aydan。
更新代码:
import shutil
import glob
import os
for filename in glob.glob(r"C:/Users/Aydan/Desktop/RTHPython/Years/*.txt"):
text_file_name = filename.strip()
dst = (os.path.join(r"C:/Users/Aydan/Desktop/SortedImages", text_file_name))
with open (text_file_name) as my_file:
for filename in my_file:
file_name = filename.strip()
src = (os.path.join('C:/Users/Aydan/Desktop', file_name))
src = src.replace('/', '\\')
shutil.move(src, dst)
这是一个新错误:
Traceback (most recent call last):
File "C:\Users\Aydan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 544, in move
os.rename(src, real_dst)
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\data01\\BL\\ER\\D11\\fmp000005578\\BL_ER_D11_fmp000005578_0001_1.txt' -> 'C:/Users/Aydan/Desktop/RTHPython/Years\\1855.txt'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#11>", line 9, in <module>
shutil.move(src, dst)
File "C:\Users\Aydan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 558, in move
copy_function(src, real_dst)
File "C:\Users\Aydan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 257, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\Aydan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 120, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\data01\\BL\\ER\\D11\\fmp000005578\\BL_ER_D11_fmp000005578_0001_1.txt'
答案 0 :(得分:0)
import shutil
import glob
import os
import ntpath
for filename in glob.glob(r"C:/Users/Aydan/Desktop/RTHPython/Years/*.txt"):
text_file_name = filename.strip()
text_name_folder = ntpath.basename(filename)
dst = os.path.join(r"C:/Users/Aydan/Desktop/SortedImages", text_name_folder.strip(".txt"))
dst = dst.replace('/', '\\')
with open (text_file_name) as my_file:
for filename in my_file:
file_name = filename.strip()
src = (os.path.normpath('C:/Users/Aydan/Desktop' + file_name))
src = src.replace('/', '\\')
shutil.move(src, dst)
有效!