Novotose
Novotose

Reputation: 23

Finding the place to put .txt files in Python to open - using Ubuntu 15

I'm really new to programming and I've been getting on with Python3 and please with my progress even if I'm all set up a little wonky still.

The tutorial that I am working on requests me to perform

my_file = open(welcome.txt)

but when I press enter I get this...

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    my_file = open(welcome.txt)
NameError: name 'welcome' is not defined

I read on another forum that the file needed to be in the same location as where python executes from, but I have no idea where to find the file.

Upvotes: 1

Views: 96

Answers (3)

Byte Commander
Byte Commander

Reputation: 6756

The problem is that you forgot to enclose the file name in quotes:

my_file = open("welcome.txt")

The open function requires the file name as string as positional argument. What you did was to tell Python to use the value of the property txt of the variable welcome, which doesn't exist.

Upvotes: 0

Roman Kh
Roman Kh

Reputation: 2735

File name should be in quotes:

my_file = open("welcome.txt")

Upvotes: 0

Nick
Nick

Reputation: 7451

You have 2 issues:

1) The file name needs to be a string, so enclose it with quotes, like so: my_file = open('welcome.txt')

2) The file needs to be either in the same directory, or you need to specify where it is, like: my_file = open('/home/user/welcome.txt')

Upvotes: 2

Related Questions