0

i am trying to look for a files in directory , copy their entire windows path into a list and copy the those files on the temp folder. with the below code i am only able to copy the file name. i have tried using pathlib module but it keeps throwing up a name error when i run it in command prompt . any suggestions would be helpful .

I prefer to use the pathlib and shutil modules , but it doesnt run from command prompt and throws a name error .

Any suggestions would be helpful

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)

error -

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
prdp13
  • 81
  • 1
  • 8

0 Answers0