当前位置: 动力学知识库 > 问答 > 编程问答 >

Opening Text Files in Python 2.78 with Windows PowerShell

问题描述:

I have a txt file named sample.txt, and I want to open it via the Windows Powershell in Python. I want the contents of the text file to be opened and displayed within the shell. So I go to the folder that the txt file is located in, and I ran python in the shell. However, I'm not sure where to go from here. When I do

open(sample.txt)

or

sample.txt.read()

I get

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name 'ex15_sample' is not defined

I've tried Googling, but I can't find anything.

Edit: It wasn't working because I didn't include double quotes for the file name and it thought I was referring to a variable named sample.

网友答案:

you have to write the exact code on the python shell as you wrote on your ex15.py file to open the text file, Like this

txt = open('ex15_sample.txt')

txt.read()

分享给朋友:
您可能感兴趣的文章:
随机阅读: