找到你要的答案

Q:Open a txt file in python using Spyder

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?

answer1: 回答1:

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

我用Spyder在Windows和我可以打开的文件就好了如果我确定我是在正确的目录。使用下列脚本更改为正确的目录:

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