Python Forum
get all the files in the path in a list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get all the files in the path in a list?
#21
(Jul-15-2021, 06:35 AM)korenron Wrote: what do I need to change \ do ?
Not all system take pathlib object,then need to convert to normal string(str object).
Use fspath for this as is made just for this task.
from pathlib import Path
from os import fspath

dest = r'G:\div_code\all_files'
for path in Path(dest).rglob('*'):
    if path.is_file():
        print(fspath(path))
Use type() if you unsure what object used.
from pathlib import Path
from os import fspath

dest = r'G:\div_code\all_files'
for path in Path(dest).rglob('*'):
    if path.is_file():
        print(type(fspath(path)))
Output:
<class 'str'> <class 'str'> <class 'str'> <class 'str'>
Reply
#22
def upload_script(routerIP, File_Name_To_Upload):
    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)
this is my fucntion that should read the files and upload them using FTP
Reply
#23
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}')
Reply
#24
I understand
and I have foudn the problem , like you suggested
I needed to change to this
Upload_Status = upload_script(ip, str(test_file))
make the 'file' a string and not a path

Thank you ,
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  does not save in other path than opened files before icode 3 887 Jun-23-2023, 07:25 PM
Last Post: snippsat
  list the files using query in python arjunaram 0 666 Mar-28-2023, 02:39 PM
Last Post: arjunaram
  How to download a list of files from FTP? schnarkle 0 1,000 Jun-21-2022, 10:35 PM
Last Post: schnarkle
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,195 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Put all files in a directory into list. How? Denial 2 2,119 Sep-18-2020, 10:05 PM
Last Post: Larz60+
  Python - List from csv files Wscwt011 1 1,827 Mar-18-2020, 06:22 PM
Last Post: alpho
  How to list out specific excel files ajay_pal7 2 2,792 Mar-10-2020, 05:43 AM
Last Post: Larz60+
  How to get full path of specified hidden files matching pattern recursively SriRajesh 4 3,906 Jan-18-2020, 07:12 PM
Last Post: SriRajesh
  Downloading And Saving Zip Files To A Particular Path Folder eddywinch82 2 2,547 Jan-06-2020, 07:56 PM
Last Post: eddywinch82
  unable to list files in a directory christober 2 2,031 Sep-18-2019, 11:45 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020