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?
#11
what about my issue ?
how can I get the all files in a spesific folder (include sub-folder)?
can't be the only one how need it \ thought about it :-)

Thanks ,
Reply
#12
What exactly is wrong with using iterdir on the Path object?
Reply
#13
this is what I get:

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
#14
Hi, and thanks for your reply! I just want to learn how this works!

I am running Ubuntu, I never use Windows, I find it terrible.

In my path are 978 files to be found, according to my answer using glob.

I tried your code in my Idle shell.

Quote:>>> from pathlib import Path
>>> mydir = Path(path)
>>> filelist = [filename for filename in mydir.iterdir() if filename.is_file()]
>>> 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}")


>>> len(filelist)
0
>>> path
'/home/pedro/summer2021/OMR/'
Reply
#15
(Jul-11-2021, 01:39 PM)korenron Wrote: how can I get the all files in a spesific folder (include sub-folder)?
Then need a recursive solution with pathlib it will be like this.
from pathlib import Path

dest = r'C:\Test'
for path in Path(dest).rglob('*'):
    if path.is_file():
        print(path)
With OS module the same would be.
import os

dest = r'C:\Test'
for root,dirs,files in os.walk(dest):
    for file in files:
        print(os.path.join(root, file))
Pedroski55 likes this post
Reply
#16
Both work great for me!

Can I ask why you use r ??

Quote:r'C:\Test'

I tried with and without r, both work for me on Ubuntu.
Reply
#17
Great
work like a charm , like I needed to :-)

what is the dfferent between the two?

I used the first one - do I need to check the second one also ?

Thanks ,
Reply
#18
(Jul-12-2021, 02:18 AM)Pedroski55 Wrote: Can I ask why you use r ??
To avoid problem with escape character
>>> path = 'C:\test'
>>> print(path)
C:      est
>>> path = r'C:\test'
>>> print(path)
C:\test
Can also write it like this C:\\test or other way C:/ both work,just not singe \.
On Linux usually don't have this problem.

korenron Wrote:I used the first one - do I need to check the second one also ?
No,the first is the newest and preferred one with pathlib tutorial
It was just to show how it was done with OS module.
Reply
#19
Hi,
I have anothe problem now...
'WindowsPath' object is not subscriptable
when I try to upload it using FTP I get the above error
 try:
        for test_file in Path(Test_Path).rglob('*'):
            if test_file.is_file():
                Upload_Status = upload_script(ip, test_file)
what do I need to change \ do ?
Reply
#20
Where are you treating the path like a sequence and shouldn't be? If you want more specific help, you need to give more details. Specifically:

- Show the full traceback. It gives important information about where the error occurred (i.e. the line number)
- Show all the code, or a minimal, yet complete example that demonstrates the problem (the code you've provided is not complete, as some variables and functions are not defined).
Reply


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