Posts: 257
Threads: 108
Joined: May 2019
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 ,
Posts: 1,838
Threads: 2
Joined: Apr 2017
What exactly is wrong with using iterdir on the Path object?
Posts: 257
Threads: 108
Joined: May 2019
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"]
Posts: 1,093
Threads: 143
Joined: Jul 2017
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/'
Posts: 7,315
Threads: 123
Joined: Sep 2016
(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
Posts: 1,093
Threads: 143
Joined: Jul 2017
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.
Posts: 257
Threads: 108
Joined: May 2019
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 ,
Posts: 7,315
Threads: 123
Joined: Sep 2016
Jul-12-2021, 08:41 AM
(This post was last modified: Jul-12-2021, 08:41 AM by snippsat.)
(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.
Posts: 257
Threads: 108
Joined: May 2019
Jul-15-2021, 06:35 AM
(This post was last modified: Jul-15-2021, 06:35 AM by korenron.)
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 ?
Posts: 1,838
Threads: 2
Joined: Apr 2017
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).
|