To put together i would do it like this.
So a own function that
yield
files that paths now convert to strings
from pathlib import Path
from os import fspath
def read_files(path_os):
for path in Path(path_os).rglob('*'):
if path.is_file():
yield fspath(path)
def upload_script(file_upload):
for file in file_upload:
print(file)
'''UploadOk = "Problem"
ftp = FTP(routerIP, timeout=60)
try:
ftp.login(user=username, passwd=password)
except error_perm as msg:
print(f"FTP error: {msg}")
return "Wrong User or Password"
except Exception as e1:
print(e1)
print("Error in FTP Login!")
return "Unknown Error"
else:
try:
ftp.storbinary('STOR ' + nameOnRouter, open(filename, 'rb'))
UploadOk = "OK"
return "FTP success"
except Exception as e:
print('my error')
print(e)
UploadOk = "Problem"
return "FTP Error " + str(e)
finally:
print('done with ' + nameOnRouter + " status " + UploadOk)'''
if __name__ == '__main__':
path_os = r'G:\div_code\all_files'
path_os = read_files(path_os)
upload_script(path_os)
Output:
G:\div_code\all_files\all_file_req.py
G:\div_code\all_files\Nytt Adobe Photoshop Image 22.psd
G:\div_code\all_files\foo\Articles.csv
G:\div_code\all_files\Ny mappe\Nytt tekstdokument.txt
Try to not use CamelCase in Python write it like this.
UploadOk # No
upload_ok # yes
Look into
f-string.
# No
print('done with ' + nameOnRouter + " status " + UploadOk)
# Yes
print(f'done with {name_on_router} status {upload_ok}')