Python Shutil 移动失败:找不到文件
Python Shutil move failing: File Not Found
我在下面的代码中收到一个错误,上面写着 FileNotFoundError: [WinError 2] The system cannot find the file specified: 'Harry White.txt' -> 'C:\Users\johna\Desktop\z_testingmove\Harry White\Harry White.txt'
谁能帮帮我?
import shutil
import os, sys
source = 'C:\Users\johna\Desktop\z_testingmove'
dest1 = 'C:\Users\johna\Desktop\z_testingmove\Harry White'
dest2 = 'C:\Users\johna\Desktop\z_testingmove\John Smith'
dest3 = 'C:\Users\johna\Desktop\z_testingmove\Judy Jones'
files = os.listdir(source)
for f in files:
if f == "Harry White.txt":
shutil.move(f, dest1)
elif f == "John Smith.txt":
shutil.move(f, dest2)
elif f == "Judy Jones.txt":
shutil.move(f, dest3)
您对 shutil.move
函数的理解有误。
shutil.move(src, dst)
Recursively move a file or directory (src) to another location (dst)
and return the destination.
src
和dst
必须是文件或目录的full path
。
您必须更改代码,试试这个:
import shutil
import os, sys
source = 'C:\Users\johna\Desktop\z_testingmove'
dest1 = 'C:\Users\johna\Desktop\z_testingmove\Harry White'
dest2 = 'C:\Users\johna\Desktop\z_testingmove\John Smith'
dest3 = 'C:\Users\johna\Desktop\z_testingmove\Judy Jones'
files = os.listdir(source)
for filename in files:
sourcepath = os.path.join(source, filename)
if filename == "Harry White.txt":
destpath = os.path.join(dest1, filename)
shutil.move(sourcepath, destpath)
elif filename == "John Smith.txt":
destpath = os.path.join(dest2, filename)
shutil.move(sourcepath, destpath)
elif filename == "Judy Jones.txt":
destpath = os.path.join(dest3, filename)
shutil.move(sourcepath, destpath)
我在下面的代码中收到一个错误,上面写着 FileNotFoundError: [WinError 2] The system cannot find the file specified: 'Harry White.txt' -> 'C:\Users\johna\Desktop\z_testingmove\Harry White\Harry White.txt'
谁能帮帮我?
import shutil
import os, sys
source = 'C:\Users\johna\Desktop\z_testingmove'
dest1 = 'C:\Users\johna\Desktop\z_testingmove\Harry White'
dest2 = 'C:\Users\johna\Desktop\z_testingmove\John Smith'
dest3 = 'C:\Users\johna\Desktop\z_testingmove\Judy Jones'
files = os.listdir(source)
for f in files:
if f == "Harry White.txt":
shutil.move(f, dest1)
elif f == "John Smith.txt":
shutil.move(f, dest2)
elif f == "Judy Jones.txt":
shutil.move(f, dest3)
您对 shutil.move
函数的理解有误。
shutil.move(src, dst)
Recursively move a file or directory (src) to another location (dst) and return the destination.
src
和dst
必须是文件或目录的full path
。
您必须更改代码,试试这个:
import shutil
import os, sys
source = 'C:\Users\johna\Desktop\z_testingmove'
dest1 = 'C:\Users\johna\Desktop\z_testingmove\Harry White'
dest2 = 'C:\Users\johna\Desktop\z_testingmove\John Smith'
dest3 = 'C:\Users\johna\Desktop\z_testingmove\Judy Jones'
files = os.listdir(source)
for filename in files:
sourcepath = os.path.join(source, filename)
if filename == "Harry White.txt":
destpath = os.path.join(dest1, filename)
shutil.move(sourcepath, destpath)
elif filename == "John Smith.txt":
destpath = os.path.join(dest2, filename)
shutil.move(sourcepath, destpath)
elif filename == "Judy Jones.txt":
destpath = os.path.join(dest3, filename)
shutil.move(sourcepath, destpath)