How can we save a text file we make using python in another directory?The default directory is always the directory in which , the program is ? is there any way to change this?Can this be done when we are creating the file (FILE=open(filename,"w"))

thanks in advance

Dani AI

Generated

You do not need to change the working directory to save to another location; just pass an absolute or well-formed relative path. On modern Python, prefer pathlib to build paths portably, create missing folders, and avoid separator issues (Windows \ vs /). Also use a context manager and explicit encoding to avoid resource leaks and locale surprises.

from pathlib import Path

# Example: write to a subfolder in the user's home directory
out_dir = Path.home() / "reports" / "2025"
out_dir.mkdir(parents=True, exist_ok=True)  # ensure folders exist

file_path = out_dir / "summary.txt"
with file_path.open("w", encoding="utf-8") as f:
    f.write("Report contents...\n")

# If you need a path relative to the script file:
# script_dir = Path(__file__).resolve().parent
# file_path = script_dir / "data" / "output.txt"

Notes:

  • On Windows, Python APIs accept forward slashes, and pathlib normalizes paths for you. Avoid manual string concatenation for paths.
  • Mode "w" overwrites; use "x" to fail if the file already exists, or "a" to append.
  • Avoid os.chdir() in larger apps; it is process-wide and can break relative paths elsewhere. Prefer absolute paths.

Docs: pathlib, open() modes and encoding.

Recommended Answers

All 5 Replies

How can we save a text file we make using python in another directory?The default directory is always the directory in which , the program is ? is there any way to change this?Can this be done when we are creating the file (FILE=open(filename,"w"))

thanks in advance

Instead of filename, you can give a path to a file in another directory to open. For example

from os.path import join as pjoin
filename = "myfile.txt"
path_to_file = pjoin("C:", "foo", "bar", "baz", filename)
FILE = open(path_to_file, "w")

Gribouillis

I'm not sure if this has been fixed or not (perhaps you're using a newer version of Python where this bug has been eliminated) but when I use your path.join this is what I get:

>>> from os.path import join as pjoin
>>> pjoin("C:", "foo", "bar", "baz")
'C:foo\\bar\\baz'
>>>

This is in Python 2.6.2 and has always been this way. I'm not sure why, but I've always found that when using a drive letter in windows you always have to explicitly add backslashes like this:

>>> from os.path import join as pjoin
>>> pjoin("C:\\", "foo", "bar", "baz")
'C:\\foo\\bar\\baz'
>>>

Ok, thanks for the correction.

In your case it might be simpler to hard code the thing ...

filepath = "C/myfolder/myfile.txt"
fout = open(filepath, "w")

thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.