Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telling Python to save a .txt file to a certain directory on Windows and Mac

Tags:

python

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() 
like image 507
user1031493 Avatar asked Nov 06 '11 00:11

user1031493


People also ask

How do you create .TXT file in a specific folder in Python?

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.

How do I change where Python saves files?

Right-click on the IDLE shortcut and select properties. Set the "Start in" directory to be where you want default save path to be.

How does Python 3 save data as txt?

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.

How to open and write a text file in Python?

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:

How to save a file in a folder in Python?

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.

What is the use of context manager in Python?

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.


2 Answers

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!

like image 113
Acorn Avatar answered Oct 06 '22 20:10

Acorn


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/~' 
like image 41
unutbu Avatar answered Oct 06 '22 18:10

unutbu