How do you tell Python where to save a text file?
For example, my computer is running the Python file off my desktop. I want it to save all the text file in my documents folder, not on my desktop. How do I do that in a script like this?
name_of_file = raw_input("What is the name of the file: ") completeName = name_of_file + ".txt" #Alter this line in any shape or form it is up to you. file1 = open(completeName , "w") toFile = raw_input("Write what you want into the field") file1.write(toFile) file1.close()
To create text files in python, you can use the open(“filename”, “accessmode”) function. The below code will create a file named mydocument. txt with write access permissions. This file will get created under the folder where the code is getting executed.
Right-click on the IDLE shortcut and select properties. Set the "Start in" directory to be where you want default save path to be.
Python3 saves data as a txt file, the specific content is as follows: f =open ("data/model_Weight.txt",'a') #If the file does not exist, the system automatically creates it.'a'Indicates that it can be written to the file continuously, and the original content is retained.
Use python's built-in open () class to open the text file, write data to the file, use the write () function, after writing, use the close () function to close and save the text file Python3 saves data as a txt file, the specific content is as follows:
If you want save file or another some in folder you can use shutil module from base python library. All your choised files will put in directed folder. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.
These methods allow you to write either a single line at a time or write multiple lines to an opened file. While Python allows you to open a file using the open (), it’s best to use a context manager to more efficiently and safely handle closing the file.
Just use an absolute path when opening the filehandle for writing.
import os.path save_path = 'C:/example/' name_of_file = raw_input("What is the name of the file: ") completeName = os.path.join(save_path, name_of_file+".txt") file1 = open(completeName, "w") toFile = raw_input("Write what you want into the field") file1.write(toFile) file1.close()
You could optionally combine this with os.path.abspath()
as described in Bryan's answer to automatically get the path of a user's Documents folder. Cheers!
Use os.path.join to combine the path to the Documents
directory with the completeName
(filename?) supplied by the user.
import os with open(os.path.join('/path/to/Documents',completeName), "w") as file1: toFile = raw_input("Write what you want into the field") file1.write(toFile)
If you want the Documents
directory to be relative to the user's home directory, you could use something like:
os.path.join(os.path.expanduser('~'),'Documents',completeName)
Others have proposed using os.path.abspath
. Note that os.path.abspath
does not resolve '~'
to the user's home directory:
In [10]: cd /tmp /tmp In [11]: os.path.abspath("~") Out[11]: '/tmp/~'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With