This question already has answers here:
How to delete a file by extension in Python?
(7 answers)
Closed 4 years ago.
want to delete pdf files from the directory using python.
Having "pdffiles" name folder in that lost of pdf are there So I want to delete all files from it but don't want to delete folder, want to delete just file from folder. how can I do it.(may be using os.remove())
Try listdir+remove:
import os
for i in os.listdir('directory path'):
if i.endswith('.pdf'):
os.remove(i)
Related
This question already has answers here:
How to move a file in Python?
(11 answers)
Closed 2 years ago.
I already know how to create and write a text file in Python, but how do I move that file to a different folder on my system?
You can either make a system call with os.system:
import os
os.system("mv /path/to/file /path/to/destination")
or rename it:
os.rename("/path/to/file", "/path/to/destination")
or move it with `shutil.move`:
```python
import shutil
shutil.move("/path/to/file", "/path/to/destination")
First solution works only in bash shells, second and third should be portable over all platforms. Third has the advantage that you can specify a folder as destination, the file will then be put into that folder with the same name as in the old location.
This question already has answers here:
Python: Open file in zip without temporarily extracting it
(4 answers)
Closed 5 years ago.
I have a url link for a zip file. I want to download the zip file. Then I want to list the name of all files that are in the zip file. One of them is a .csv file. I also want to read from the csv file.
Can anybody tell me how I can do it in python3?
urllib.request.retrieve to download zip file
https://docs.python.org/3/library/urllib.request.html
zipfile module to extract files https://docs.python.org/3/library/zipfile.html
find csv file(s) in path with glob module https://docs.python.org/3/library/glob.html
finally use csv module
https://docs.python.org/3/library/csv.html
This question already has answers here:
How to rename a file using Python
(17 answers)
Closed 6 years ago.
I have a folder of log files recorded as XML. A guy in here made a mess in there and changed the script of creation of those files and insert a 'p' letter at the beginning. E.g.:
The file name is now: p0154656450103.xml
When it should be: 0154656450103.xml
There are about 3,000 files this way. How can I rename them erasing just the first character or just the 'p' character?
You can use os.rename:
import os
os.rename(src, dst)
This question already has answers here:
Directory-tree listing in Python
(21 answers)
Closed 9 years ago.
I am writing a function that is recieving a folder path as an arguemnt. I want her to add into a dictionary what's inside the folder (like dir in CMD)
How can I do this ?
Thank you in advance,
Iliya
import os
print os.listdir('/tmp')
Similar Topics:
Directory listing in Python
Also, I use os.path and glob a lot while manipulating file system path.
You might want to check it out.
This question already has answers here:
reading tar file contents without untarring it, in python script
(4 answers)
Closed 2 years ago.
I've got a huge *.tar.gz file and I want to see the list of files contained in it without extracting the contents (preferably with mtimes per file). How can I achieve that in python?
You can use TarFile.getnames() like this:
#!/usr/bin/env python3
import tarfile
tarf = tarfile.open('foo.tar.gz', 'r:gz')
print(tarf.getnames())
http://docs.python.org/3.3/library/tarfile.html#tarfile.TarFile.getnames
And if you want mtime values you can use getmembers().
print([(member.name, member.mtime) for member in tarf.getmembers()])