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?
#1
Hello ,
I forgot how to write a path so I will have a list of all my files insside?

example will do the job: :-)
right now I'm using this line
Files_Path = ["C:\\Test\\App1\\hotspot\\Images\\Bunner.png", "C:\\Test\\App1\\login.html",
              "C:\\Test\\App1\\script.txt"]
then I use this:
        for file in Files_Path:
              Upload_Status = upload_script(ip, file)
what do I need to do\cahnge so I will not have to write all the files name by name , and it will just take the all file paths inside the wanted directory ?
*** if next I will need to uplaod 15 files , I don't want to write them by name - jsut give the 'root" directorry
in this case
C:\Test\App1\
and thats all

Thanks ,
Reply
#2
That's what the library reference is for. You can look at the pathlib or os modules.
Reply
#3
I am not an expert, but this works for me:

split any path on '.' a file will give a resulting list of len(resultlist) = 2

If you can split the path, it is a file or a ./ a hidden directory

path = '/home/pedro/summer2021/OMR/'

files = glob.glob(path + '**', recursive=True)
for f in files:
    # shows all paths and files in path
    print(f)

uploads = []

for f in files:
    # if the path leads to a file, it will contain a .
    # problem is if there are hidden directories with naked pictures of your gf I think
    if len(f.split('.')) == 2: # you found a path with an ending .something
        uploads.append(f)
Then upload everything in uploads.
Reply
#4
this is what I have try to do:
from pathlib import Path
Test_Path = Path("C:\\Test\\")

 for file in Test_Path:
        print(file)
but I get TypeError:
for file in Test_Path:
TypeError: 'WindowsPath' object is not iterable
and if I can't even print it - I guess I will have problem working with him for FTP uplaod
Reply
#5
Did you see the iterdir method on Path? It's literally in the second example under "Basic use".
Reply
#6
can't say I did........ Blush Blush Blush Blush

but if I have files in subfolder
It won't print me \ get me the files
just print me the fodler
so what need to be do in order to tell him to get all he files in the sublfolders?

this is what I get now:

hotspot
script.txt
and in the hostspot fodler there are 5 fiels

in the end I need to get this kind of list - so I will be abel to use the full name and uplaod it FTP
["C:\\Test\\App1\\hotspot\\Images\\Bunner.png", "C:\\Test\\App1\\login.html", "C:\\Test\\App1\\script.txt"]
Reply
#7
example using iterdir:

from pathlib import Path

mydir = Path('./data/json')

filelist = [filename for filename in mydir.iterdir() if filename.is_file()]

# Example:
for filename in filelist:
    print(f"\nfilename: {filename.name}")
    print(f"file suffix: {filename.suffix}")
    print(f"full path: {filename.resolve()}")
    print(f"filepath parts: {filename.parts}")
Results (ellipsis is for personal path part I removed)
Output:
filename: walmart.json file suffix: .json full path: /media/.../data/json/walmart.json filepath parts: ('data', 'json', 'walmart.json') filename: walmart_1.json file suffix: .json full path: /media/.../data/json/walmart_1.json filepath parts: ('data', 'json', 'walmart_1.json')
Reply
#8
@Larz60+ Can you tell me please why I get nothing??

Quote:>>> mydir = Path('/home/pedro/summer2021/OMR/')
>>> filelist = [filename for filename in mydir.iterdir() if filename.is_file()]
>>> len(filelist)
0
>>>
Reply
#9
are there indeed regular files in that directory?
Also, you are running from an interactive python, which shouldn't matter, but let me take a look.
I assume that you did include the import of pathlib.Path, is that correct?
Let me play a bit and I'll get back.
Reply
#10
It works for me in interactive interpreter:
>>> from pathlib import Path
>>> mydir = Path('/media/.../data/json')
>>> filelist = [filename for filename in mydir.iterdir() if filename.is_file()]
>>> print(len(filelist))
2
>>> for filename in filelist:
...     print(filename.name)
... 
walmart.json
walmart_1.json
as before, I replaced part of the path with ellipsis (for display only).

Another possibility is slash direction, if you are on microsoft, could be an issue.
and the is_file() qualifier will only return normal files as defined in https://docs.python.org/3/library/pathlib.html

try eliminating the condition and see if you get results filelist = [filename for filename in mydir.iterdir()]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  does not save in other path than opened files before icode 3 1,062 Jun-23-2023, 07:25 PM
Last Post: snippsat
  list the files using query in python arjunaram 0 736 Mar-28-2023, 02:39 PM
Last Post: arjunaram
  How to download a list of files from FTP? schnarkle 0 1,068 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,283 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Put all files in a directory into list. How? Denial 2 2,231 Sep-18-2020, 10:05 PM
Last Post: Larz60+
  Python - List from csv files Wscwt011 1 1,924 Mar-18-2020, 06:22 PM
Last Post: alpho
  How to list out specific excel files ajay_pal7 2 2,909 Mar-10-2020, 05:43 AM
Last Post: Larz60+
  How to get full path of specified hidden files matching pattern recursively SriRajesh 4 4,093 Jan-18-2020, 07:12 PM
Last Post: SriRajesh
  Downloading And Saving Zip Files To A Particular Path Folder eddywinch82 2 2,654 Jan-06-2020, 07:56 PM
Last Post: eddywinch82
  unable to list files in a directory christober 2 2,128 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