# Q：在Python中使用“打开一个txt文件

New to python and I am using spyder in windows so I don't use bash from ubuntu or a linux system. When I use open() function it does not work?

New to python and I am using spyder in windows so I don't use bash from ubuntu or a linux system. When I use open() function it does not work?

I use Spyder on Windows and I can open files just fine if I make sure I'm in the correct directory. Use the following script to change to the correct directory:

 import os print(os.getcwd()) # see where you are os.chdir(r'Path\to\file') # use a raw string so the backslashes are ok print(os.getcwd()) # convince yourself that you're in the right place print(os.listdir()) # make sure the file is in here with open("file", 'r') as myfile: # assuming you need to read # do whatever 

 import os print(os.getcwd()) # see where you are os.chdir(r'Path\to\file') # use a raw string so the backslashes are ok print(os.getcwd()) # convince yourself that you're in the right place print(os.listdir()) # make sure the file is in here with open("file", 'r') as myfile: # assuming you need to read # do whatever 

python  spyder