i have been attempting code few days please forgive me obvious mistakes make. have searched around couple of days , have not managed find solution problem, hence why have decided post here.
my issue: have managed create piece of script search in dedicated folder , copy file, user has named themselves, via input(), multiple other folders. downfall can copy files specific extension e.g. '.docx'. code able pick named file regardless of it's extension.
here code:
import shutil, os, time while true: filename = input('\n please enter file name: ') + '.docx' try: shutil.copy('c:\\users\\guest\\desktop\\folder\\' + filename, 'c:\\users\\guest\\desktop\\employees\\folder1') shutil.copy('c:\\users\\guest\\desktop\\folder\\' + filename, 'c:\\users\\guest\\desktop\\employees\\folder2') shutil.copy('c:\\users\\guest\\desktop\\folder\\' + filename, 'c:\\users\\guest\\desktop\\employees\\folder3') except filenotfounderror: print("no such file exists. please try again.") else: break print("file transfer complete.") time.sleep(2) quit()
thanks in advance help!
you need make sure filename
either equals file's name or startswith file's name + dot. else cannot exclude errors possible files without extensions, or files same partial name.
furthermore i'd suggest use conditional rather try
/except
; action if file exists, else retry input:
import shutil, os, time while true: dr = 'c:\\users\\guest\\desktop\\folder\\' filename = input('\n please enter file name: ') # see if file exists (listing files matching conditions) match = [f f in os.listdir(dr) if any([f == filename, f.startswith(filename+".")])] # if list not empty, use first match, copy , break if match: match = match[0] shutil.copy(os.path.join(dr, match), os.path.join('c:\\users\\guest\\desktop\\employees\\folder1', match)) # etc... break else: print("no such file exists. please try again.") print("file transfer complete.") time.sleep(2) quit()
Comments
Post a Comment