Ich habe ein kleines Skript mit dem Zweck geschrieben, um die gesamte C des Suchens: \ eine bestimmten Dateierweiterung und Kopieren in einen Ordner Ihrer Wahl:Spezifische Dateierweiterung Bewegt Fehler
import os
import shutil
def move_all_ext(extension, source_root, dest_dir):
# Recursively walk source_root
for (dirpath, dirnames, filenames) in os.walk(source_root):
# Loop through the files in current dirpath
for filename in filenames:
# Check file extension
if os.path.splitext(filename)[-1] == extension:
print("copying file: %s" % filename)
# Move file
shutil.copy(os.path.join(dirpath, filename), os.path.join(dest_dir, filename))
print('Welcome to the file find/copy module\n')
print('You will be asked to enter a directory name and the file type that you want to copy\n')
# Prompt new folder name
print('Enter new directory name:')
newDir = input()
print('Enter the file extension (.pdf, .txt, etc..)')
fileExt = input()
# File directory path
os.makedirs(os.path.join('C:\\Users\\Nick\\Documents\\', newDir))
# Move all specific files from C:\ to C:\Users\Nick\Documents\
move_all_ext('.' + fileExt, "C:\\", os.path.join('C:\\Users\\Nick\\Documents\\', newDir))
Während des Laufes Skript ich habe ein paar Fehler, die mich nicht vollständig verstehen:
Traceback (most recent call last): File
"C:\Users\Nick\Documents\programming\Python\ext_copy_dest.py", line
36, in <module>
move_all_ext('.' + fileExt, "C:\\", os.path.join('C:\\Users\\Nick\\Documents\\', newDir)) File
"C:\Users\Nick\Documents\programming\Python\ext_copy_dest.py", line
20, in move_all_ext
shutil.copy(os.path.join(dirpath, filename), os.path.join(dest_dir, filename)) File
"C:\Users\Nick\AppData\Local\Programs\Python\Python35-32\lib\shutil.py",
line 235, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks) File "C:\Users\Nick\AppData\Local\Programs\Python\Python35-32\lib\shutil.py",
line 115, in copyfile
with open(dst, 'wb') as fdst: PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Nick\\Documents\\PDF\\README.txt'
ich sein ein Problem mit einem Symlink geh mal davon aus, ist es eine Möglichkeit, eine IF-Anweisung zu schreiben ‚verweigert Permission‘ alle ignorieren Fragen?
Verwenden 'Anprobe außer PermissionError' anstelle einer if-Anweisung. – Evert
Ich fühle mich albern, dass ich das nicht herausgefunden habe, danke, dass es funktioniert hat – chilledheat