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 ,
That's what the library reference is for. You can look at the
pathlib
or
os
modules.
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.
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
Did you see the iterdir
method on Path
? It's literally in the second example under "Basic use".
can't say I did........
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"]
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')
@
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
>>>
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.
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()]