Create text file in folder 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()

Create text file in folder python

asked Nov 6, 2011 at 0:01

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!

Create text file in folder python

Mehdi Nellen

7,8664 gold badges32 silver badges48 bronze badges

answered Nov 6, 2011 at 0:02

AcornAcorn

47.4k26 gold badges128 silver badges171 bronze badges

0

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/~'

Create text file in folder python

answered Nov 6, 2011 at 0:04

unutbuunutbu

797k170 gold badges1720 silver badges1622 bronze badges

A small update to this. raw_input() is renamed as input() in Python 3.

Python 3 release note

Create text file in folder python

xrisk

3,63020 silver badges41 bronze badges

answered Sep 20, 2018 at 12:26

der_radlerder_radler

5074 silver badges15 bronze badges

Another simple way without using import OS is,

outFileName="F:\\folder\\folder\\filename.txt"
outFile=open(outFileName, "w")
outFile.write("""Hello my name is ABCD""")
outFile.close()

answered Dec 30, 2018 at 1:48

Create text file in folder python

Paul ThomasPaul Thomas

4677 silver badges15 bronze badges

If you want to save a file to a particular DIRECTORY and FILENAME here is some simple example. It also checks to see if the directory has or has not been created.

import os.path
directory = './html/'
filename = "file.html"
file_path = os.path.join(directory, filename)
if not os.path.isdir(directory):
    os.mkdir(directory)
file = open(file_path, "w")
file.write(html)
file.close()

Hope this helps you!

answered Dec 3, 2019 at 3:45

Create text file in folder python

AsherAsher

2,5086 gold badges26 silver badges39 bronze badges

Using an absolute or relative string as the filename.

name_of_file = input("What is the name of the file: ")
completeName = '/home/user/Documents'+ name_of_file + ".txt"
file1 = open(completeName , "w")
toFile = input("Write what you want into the field")
file1.write(toFile)
file1.close()

answered Oct 11, 2018 at 10:20

Create text file in folder python

1

Just give your desired path if file does not exist earlier;

        from os.path import abspath
        with open ('C:\\Users\\Admin\\Desktop\\results.txt', mode = 'w') as final1:
            print(final1.write('This is my new file.'))

        print(f'Text has been processed and saved at {abspath(final1.name)}')

Output will be:

Text has been processed and saved at C:\Users\Admin\Desktop\results.txt

answered Jun 30, 2020 at 4:03

Create text file in folder python

If you want save file or another some in folder you can use shutil module from base python library.

import shutil

path_to_file = f'home/user/My_Project/media/{my_file_name}'
path_to_folder = 'home/user/my_folder/'
   
shutil.copy(path_to_file, path_to_folder)

You can do it ever for some file's list or in Django querysets:

from images.models import Image
import shutil

images = Image.objects.all()
for image in images:
    image_path = image.file.path
    shutil.copy(image_path, path_to folder)
   

All your choised files will put in directed folder.

answered Mar 17, 2021 at 15:52

How do you create a txt file in Python?

Summary. Use the function open(“filename”,”w+”) for Python create text file. The + tells the python interpreter for Python open text file with read and write permissions. Use the readlines function to read the content of the file one by one.

How do I create a text file in a directory?

Another way to create a text file is to right-click an empty area on the desktop, and in the pop-up menu, select New, and then select Text Document. Creating a text file this way opens your default text editor with a blank text file on your desktop. You can change the name of the file to anything you want.

How do you save and create a text file in Python?

Python File Write.
❮ Previous Next ❯.
Example. Open the file "demofile2.txt" and append content to the file: f = open("demofile2.txt", "a") f.write("Now the file has more content!") f.close() ... .
Example. Open the file "demofile3.txt" and overwrite the content: f = open("demofile3.txt", "w") f.write("Woops! ... .
❮ Previous Next ❯.

How do I save a file in a specific directory in Python?

How to write a file to a specific directory in Python.
save_path = '/home'.
file_name = "test.txt".
completeName = os. path. join(save_path, file_name).
print(completeName).
file1 = open(completeName, "w").
file1. write("file information").
file1. close().