file=open(words.txt)
print(file)

Please Ive been trying to use the open() function to open a txt file in python, but it keeps telling me that there is no such file or directory, where should I put the file for me to be able to import it.
HINT:
I use pyscripter, but it's the portable version, the program i run is in the portable python folder then their is an App folder there.
Thanks in Advance

I think I've seen another mistake of mine instead of

file=open('words.txt')
print(file)

I wrote this

file=open(words.txt)
print(file)

but i still need help as to where I put the txt files.

import os
os.chdir('/some/path')
print(os.listdir(os.curdir))

Hope this is OK, i type this from my
mobile and I have not possibility to run it now.

I'll check and let you know. Thanks so much.

Dont use file as variable is a reserved keyword for python.
So for this script under to work,test.py an words.txt need to be in same folder.

#test.py
my_file = open('words.txt') #Remeber '' open take string as argument
print my_file.read()
my_file.close()

Let say that words.txt is not in same folder as test.py
Then you need to give correct path to words.txt
Here words.txt is placed in folder c:\test
Remeber not use single \ when give path to file,python can see \ as an escape character.
c:\\test or c:/test not c:\test

#test.py
my_file = open('c:/test/words.txt')
print my_file.read()
my_file.close()

The most common way in python today is to use with open()

with open('words.txt') as f:
    print f.read()

With this method you dont need to close() file object,as you see i do in script over.

commented: Thanks +1

Thanks got the first person but it seems your methods makes things easier, will try it out. Thanks

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.