This question already has answers here:
Where does os.remove go?
(2 answers)
Closed 1 year ago.
I have a more general question about the os.remove() -function.
When I use it to delete a file, does Python delete it entirely from the computer or does Python move it to another folder similar to the trash on mac?
os.remove doesn't send things to Trash. It just deletes them.
Here you go for os.remove docs: https://docs.python.org/3/library/os.html#os.remove
Also i notice similar post on stackoverflow: How to permanently delete a file in python 3 and higher?
os.remove() doesn't send things to Trash. It just deletes them.
You can check it yourself very easily:
import os
file = 'file.txt'
location = "/home/User/Documents"
path = os.path.join(location, file)
os.remove(path)
print("%s has been removed successfully" % file)
Related
This question already has answers here:
python - Finding the user's "Downloads" folder
(6 answers)
Closed 2 months ago.
I am wanting to set up a system of CSV formatters in conjunction with a PySimpleGUI program. I want the output file to go to the users Downloads folder, but currently I know how to only use the Path method for my own Downloads folder. If I packaged this up, it would not be dynamic.
path = Path(r"C:\\Users\\xxx.xxxxx\\downloads\\Finished_File.csv")
I am unsure of other ways to go about auto-filling the user info without inputting it manually
My only other thinking is perhaps have this change dynamically with PySimpleGui using a list of potential names, and then having the user set who they are?
Find it manually before saving uisng pathlib
from pathlib import Path
users_download_path = str(Path.home() / "Downloads")
res =str( users_download_path) + '\' + str('Finished_File.csv')
path = Path(res)
This question already has answers here:
How can I safely create a directory (possibly including intermediate directories)?
(28 answers)
Closed 1 year ago.
What is the best way to check if the directory a file is going to be written to exists, and if not, how to create the directory using Python?
Does a flag exists as "open", that makes this happen automatically?
Use makedirs from os module:
os.makedirs(DIRECTORY, exist_ok=True)
This will create a directory if it doesn't exist.
This question already has an answer here:
Batch File Rename with Python
(1 answer)
Closed 2 years ago.
I have a folder which have more than 100 files. The folder contains.txt files. i want to remove ".txt" extension from all files. also tried os.path.splitext even os.remove it doesn't work.
You can use os.rename(). The file name is just a string. You can remove the 4 last characters with:
src = 'path_to_txt.txt'
dst = src[:-4]
os.rename(src, dst)
You can use stem from the pathlib module.
import pathlib
file_name = pathlib.Path(‘textfile.txt‘)
file_name_wo_ext = file_name.stem
This question already has answers here:
Delete multiple files matching a pattern
(9 answers)
Closed 2 years ago.
I am new to scripting and trying to write a python script to remove a few files.. Here is the path Multiple scripts/script*
Main directory: Multiple scripts
sub directories: script1
script2
script3
In each script in subdirectories, I have file consists of mem. Since I don't know what they start with or their extension now I would like to write a script to search all the subdirectories and delete files that consists of mem.
I tried using the following code but did not work for me
import os
if Multiple scripts/scripts*
os.remove(*/mem*)
else:
print ("file does not exists")
And also please help me with how to write a script to delete files with multiple names (/mem, /name) at a time. Any help would be appreciated... Thank you
If I'm understanding your question correctly, I think you'll want the glob module. Something like
import os
import glob
for fname in glob.iglob("./*/mem*"):
print("Removing "+fname)
os.remove(fname)
Before you run that loop, though, I'd say run it first without the last line, to see what it would do, and make sure that that's what you want.
This question already has answers here:
os.makedirs doesn't understand "~" in my path
(3 answers)
Closed 7 years ago.
I am having an issue with trying to list all of the files/folders within a directory with Python 2.6, on Mac OS X.
To simplify the problem, I am attempting to simply list all the files on my desktop (which is not empty). I understand this can be done like this:
currentFileList = os.listdir("~/Desktop")
But I am getting the error:
currentFileList = os.listdir("~/Desktop")
OSError: [Errno 2] No such file or directory: '~/Desktop'
Any suggestions?
You should pass absolute pass to os.listdir function. You can use os.expanduser function to expand ~:
os.listdir(os.path.expanduser('~/Desktop'))
By the way. Be careful: ~foobar will replace the path with home folder for user foobar (e.g. /home/foobar)
You need the full path not relative
os.listdir('/Users/YOURUSERNAME/Desktop')