使用 shutil 移动文件在 python 中无法按预期工作
Moving file using shutil does not work as expected in python
尝试将文件从一个文件夹移动到另一个文件夹,但不断出现如下所示的错误。我在 sample1 源中确实有这个 Model.xlsx 文件,但我无法移动它。我尝试使用 'shutil.move(os.path.abspath(f), dest1)' 绝对路径,但这给了我不同的错误。它实际上选择了一个随机文件夹。谢谢
import shutil
import os
source = 'D:/Desktop/sample1'
dest1 = 'D:/Downloads/sample2'
dest2 = 'D:/Downloads/sample3'
files = os.listdir(source)
for f in files:
if (f.startswith("Model.xlsx")):
shutil.move(f, dest1)
elif (f.startswith("Intel") or f.startswith("intel")):
shutil.move(os.path.abspath(f), dest2)
错误:
Traceback (most recent call last):
File "D:\Users\KSanala1\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 566, in move
os.rename(src, real_dst)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'Model.xlsx' -> 'D:/Downloads/sample2\Model.xlsx'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/Users/KSanala1/PycharmProjects/pythonProject/main.py", line 16, in <module>
shutil.move(f, dest1)
File "D:\XXX\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 580, in move
copy_function(src, real_dst)
File "D:\XXX\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 266, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "D:\XXX\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 120, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'Model.xlsx'
Process finished with exit code 1
os.listdir
仅 returns 文件名列表。要获得实际路径(您需要),您需要连接原始文件路径,即 os.listdir
的参数与文件名
files = os.listdir(source)
for f in files:
fpath = os.path.join(source, f)
现在您有了一个有意义的路径,而不仅仅是一个文件名。
然而,将 source = 'D:/Desktop/sample1'
等硬编码路径与 os.path
函数混合并不是一个好主意。首先考虑使用 os.path
构建路径
source = os.path.join('D:', os.sep, 'Desktop', 'sample1')
您需要提供 shutil.move 的完整路径,在您的情况下:
os.path.join(来源,女)
files = os.listdir(source)
for f in files:
f_path = os.path.join(source,f)
if (f.startswith("Model.xlsx")):
shutil.move(f_path , dest1)
elif (f.startswith("Intel") or f.startswith("intel")):
shutil.move(os.path.abspath(f_path), dest2)
尝试将文件从一个文件夹移动到另一个文件夹,但不断出现如下所示的错误。我在 sample1 源中确实有这个 Model.xlsx 文件,但我无法移动它。我尝试使用 'shutil.move(os.path.abspath(f), dest1)' 绝对路径,但这给了我不同的错误。它实际上选择了一个随机文件夹。谢谢
import shutil
import os
source = 'D:/Desktop/sample1'
dest1 = 'D:/Downloads/sample2'
dest2 = 'D:/Downloads/sample3'
files = os.listdir(source)
for f in files:
if (f.startswith("Model.xlsx")):
shutil.move(f, dest1)
elif (f.startswith("Intel") or f.startswith("intel")):
shutil.move(os.path.abspath(f), dest2)
错误:
Traceback (most recent call last):
File "D:\Users\KSanala1\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 566, in move
os.rename(src, real_dst)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'Model.xlsx' -> 'D:/Downloads/sample2\Model.xlsx'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/Users/KSanala1/PycharmProjects/pythonProject/main.py", line 16, in <module>
shutil.move(f, dest1)
File "D:\XXX\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 580, in move
copy_function(src, real_dst)
File "D:\XXX\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 266, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "D:\XXX\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 120, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'Model.xlsx'
Process finished with exit code 1
os.listdir
仅 returns 文件名列表。要获得实际路径(您需要),您需要连接原始文件路径,即 os.listdir
的参数与文件名
files = os.listdir(source)
for f in files:
fpath = os.path.join(source, f)
现在您有了一个有意义的路径,而不仅仅是一个文件名。
然而,将 source = 'D:/Desktop/sample1'
等硬编码路径与 os.path
函数混合并不是一个好主意。首先考虑使用 os.path
构建路径
source = os.path.join('D:', os.sep, 'Desktop', 'sample1')
您需要提供 shutil.move 的完整路径,在您的情况下: os.path.join(来源,女)
files = os.listdir(source)
for f in files:
f_path = os.path.join(source,f)
if (f.startswith("Model.xlsx")):
shutil.move(f_path , dest1)
elif (f.startswith("Intel") or f.startswith("intel")):
shutil.move(os.path.abspath(f_path), dest2)