尝试将具有特定名称的文件移动到新目录
Attempting to move files with certain names to a new directory
我正在尝试将以 PS-110 开头的文件移动到我创建的名为 PS-110 的新文件夹中。
import shutil, glob, os
files_move = []
files_move = [files_move.append(f) for f in glob.glob('PS-110*.pdf')]
destination = r"C:\Users\kjurgens\Downloads\PS-110"
for f in files_move:
shutil.move(f, destination)
运行时出现以下错误:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "C:\Users\kjurgens\AppData\Local\Programs\Python\Python38-32\lib\shutil.py", line 771, in move
if _samefile(src, dst):
File "C:\Users\kjurgens\AppData\Local\Programs\Python\Python38-32\lib\shutil.py", line 217, in _samefile
return os.path.samefile(src, dst)
File "C:\Users\kjurgens\AppData\Local\Programs\Python\Python38-32\lib\genericpath.py", line 100, in samefile
s1 = os.stat(f1)
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType
如有任何意见,我们将不胜感激。
files_move = [files_move.append(f) for f in glob.glob('PS-110*.pdf')]
这不会创建文件列表,它会创建一个包含 None
的列表。这是因为 files_move.append(f)
returns None。在整个执行完成时,您正在用新列表覆盖 files_move
。
给定 glob.glob()
已经 returns 的列表,您根本不需要 files_move
。
就这样:
import shutil, glob, os
destination = r"C:\Users\kjurgens\Downloads\PS-110"
for f in glob.glob('PS-110*.pdf'):
shutil.move(f, destination)
我正在尝试将以 PS-110 开头的文件移动到我创建的名为 PS-110 的新文件夹中。
import shutil, glob, os
files_move = []
files_move = [files_move.append(f) for f in glob.glob('PS-110*.pdf')]
destination = r"C:\Users\kjurgens\Downloads\PS-110"
for f in files_move:
shutil.move(f, destination)
运行时出现以下错误:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "C:\Users\kjurgens\AppData\Local\Programs\Python\Python38-32\lib\shutil.py", line 771, in move
if _samefile(src, dst):
File "C:\Users\kjurgens\AppData\Local\Programs\Python\Python38-32\lib\shutil.py", line 217, in _samefile
return os.path.samefile(src, dst)
File "C:\Users\kjurgens\AppData\Local\Programs\Python\Python38-32\lib\genericpath.py", line 100, in samefile
s1 = os.stat(f1)
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType
如有任何意见,我们将不胜感激。
files_move = [files_move.append(f) for f in glob.glob('PS-110*.pdf')]
这不会创建文件列表,它会创建一个包含 None
的列表。这是因为 files_move.append(f)
returns None。在整个执行完成时,您正在用新列表覆盖 files_move
。
给定 glob.glob()
已经 returns 的列表,您根本不需要 files_move
。
就这样:
import shutil, glob, os
destination = r"C:\Users\kjurgens\Downloads\PS-110"
for f in glob.glob('PS-110*.pdf'):
shutil.move(f, destination)