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.
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:
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)
This question already has answers here:
Obtaining file basename with a prespecified extension in Python
(2 answers)
Closed 2 years ago.
Say, I have a folders structure with files inside. In some files, objects of a specific type can be found. I want to initiate them dynamically (I know each instance of that type has a specific method). But I don't know how I would implement it. I think the pseudocode could be something like that:
for root, dirs, files in os.walk(root_folder):
for f in files:
if os.path.splitext(f)[1] == '.py':
get_fully_name_of_the_file # like app.modules.amazon
from from_fully_name import that_instance
that_instance.my_method()
import glob
all_types=[]
for f in glob.glob("root_folder"):
all_types.append[f[f.rindex("."):]]
print(all_types)
This question already has answers here:
Creating files and directories via Python
(2 answers)
Closed 3 years ago.
I want to create a directory and then create some files in that directory.
I have already created a directory like so:
if not os.path.exists("output"):
os.mkdir("output")
How can I now access this directory and create some files using something like this open("foo.txt", "w")?
Prepend the directory name to your filename:
with open('output/foo.txt', 'w') as f:
f.write('some content')
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')