How do I open a new txt file using path

I tested your code and now I assume your code has several problems.

  1. variable name: save_path -(should be)-> path:
    Simply, the two names should be the same.
  2. Absolute path should begin with ‘/Users…’:
    This seems to be crucial.

You seem to use mac, and in the case of mac OSX, path string is like this. /Users/<username>/Desktop/....

Proper code would be like this, I think.

import os.path

path ="/Users/<your user name>/Desktop"
#If deeper, path string should be longer.

name_of_file = input("What is the name of the file: ")
completeName = os.path.join(path, name_of_file+".txt")

f = open(completeName,'a')
f.close()

I tested this code on my mac, and found it works either with a new file name or with an existing file name.
Attention: I use Python3 and input is equivalent to Python2’s raw_input.

Leave a Comment