1

I am trying to create a text file using python. Here is my code-

import sys
import os

str1="a1.txt"
file1="Documents/Inbox/"
completeName=file1+str1
name1=os.path.abspath(completeName)
myfile = open(name1, 'w')

I want to save file a1.txt in my documents folder in my home directory.I get the following error using above code-

Traceback (most recent call last):
  File "filesave.py", line 8, in <module>
    myfile = open(name1, 'w')
FileNotFoundError: [Errno 2] No such file or directory: '/home/pulkit/Documents/Documents/Inbox/a1.txt'
10
  • Does /home/pulkit/Documents/Documents/Inbox exist? Commented Sep 22, 2015 at 11:22
  • Where do you exactly to create it? Commented Sep 22, 2015 at 11:22
  • 1
    So the answer is obvious: file1="Inbox/" Commented Sep 22, 2015 at 11:25
  • 1
    You should not hard-depend on ~/Documents, anyway. While it looks like the folks at FreeDesktop failed to document the "well known" directories in a real specification (arch wiki with non-normative documentation), but the accompanying library makes it clear, that the documents directory is user configurable. Commented Sep 22, 2015 at 11:38
  • 2
    @marmeladze Using an external tool to make the file exist is wasteful and unnecessary, and clearly not the problem the OP was trying to solve anyway. (Read the question, not just the title.) For what it's worth, opening and immediately closing the file in Python would accomplish the same thing without an external process. Commented Sep 22, 2015 at 15:05

4 Answers 4

3

This code shows you how to check the path exists and expand ~ to access home directory of the user running the script.

#!/usr/bin/python
import os
dpath=os.path.join(os.path.expanduser("~"),"Documents","Inbox")
if not os.path.exists(dpath):
    os.makedirs(dpath)
fpath=os.path.join(dpath,"a1.txt")
open(fpath,"w").write("what ever you want")
Sign up to request clarification or add additional context in comments.

Comments

2

os.path.abspath() does not know which directory you want the file to exist in -- it simply uses the current directory, which seems to have been $HOME/Documents when you got the backtrace.

Either

  1. always run the script from your home directory (untenable); or
  2. specify an explicit absolute path in the script; or
  3. change the logic so the script doesn't care where it's run -- commonly this is done by creating a file in the current directory always; or by simply printing to standard output, and let the user figure out what to do with the output.

3 Comments

Thank you. What is the alternative to abspath. It seems to work in this case but if I have to save it in some other folder not in current path it will fail
How can I specify an explicit absolute path?
name1 = "/home/pulkit/Documents/Inbox/a1.txt". But this is definitely the least attractive option, to my mind.
0

In your case omit the 'Documents/' from your variable file1 setting like:

file1="Inbox/"

Comments

0

See the error

FileNotFoundError: [Errno 2] No such file or directory: '/home/pulkit/Documents/Documents/Inbox/a1.txt'

Clearly, the path evaluated is:

/home/pulkit/Documents/Documents/Inbox/a1.txt

But you have,

/home/pulkit/Documents/Inbox/a1.txt

So, change file1="Documents/Inbox/" to file1="Inbox/"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.