我试图在目录中查找文件,将其整个Windows路径复制到列表中,然后将这些文件复制到temp文件夹中。 使用下面的代码,我只能复制文件名。我试过使用pathlib模块,但是当我在命令提示符下运行它时,它总是抛出名称错误。任何的意见都将会有帮助 。
我更喜欢使用pathlib和shutil模块,但是它不能从命令提示符下运行,并且会引发名称错误。
任何建议都会有所帮助
def searching_all_files(directory):
dirpath = directory
#assert(dirpath.is_dir())
file_list = []
temp= os.path.abspath(r'C:\Temp')
for x in os.listdir(dirpath):
if 'Settlement_Report' in os.path.basename(x):
s=os.path.basename(x)
file_list.append(s)
shutil.copyfile(os.path.join(dirpath,os.path.basename(x)),os.path.join(temp,os.path.basename(x)))
return file_list
p=r"some path in single slash format"
path = os.path.abspath(p)
files = searching_all_files(path)
错误-
Traceback (most recent call last):
File "C:\Program Files\WinPython\WinPython 64bit 3.6.2.0Qt5\python-3.6.2.amd64\lib\runpy.py", line 193, in _run_module_as_main "__main__", mod_spec)
File "C:\Program Files\WinPython\WinPython 64bit 3.6.2.0Qt5\python-3.6.2.amd64\lib\runpy.py", line 85, in _run_code exec(code, run_globals)
File "C:\Program Files\WinPython\WinPython 64bit 3.6.2.0Qt5\python-3.6.2.amd64\scripts\ptpython.exe\__main__.py", line 9, in <module>
File "C:\Program Files\WinPython\WinPython 64bit 3.6.2.0Qt5\python-3.6.2.amd64\lib\site-packages\ptpython\entry_points\run_ptpython.py", line 55, in run
six.exec_(compile(open(a['<arg>'][0], "rb").read(), a['<arg>'][0], 'exec'))
File "C:\Temp\Load_files_DD.py", line 86, in <module>files = searching_all_files(path)
File "C:\Temp\Load_files_DD.py", line 17, in searching_all_files
shutil.copyfile(os.path.join(dirpath,os.path.basename(x)),os.path.join(temp,
os.path.basename(x)))
NameError: name 'shutil' is not defined