2016-07-25 77 views
0

我很確定我在mystuff文件夾中有ex15_sample.txt!爲什麼我不能在PowerShell中打開並閱讀python文件?

以下是錯誤我遇到: the powershell

IOError: [Errno 2] No such file or directory: 'ex15_sample.txt' 

這是我的代碼

from sys import argv 

script, filename = argv 

txt = open(filename)  
print "Here's your file %r:" % filename 
print txt.read()      
print "Type the filename again:" 
file_again = raw_input("> ")   

txt_again = open(file_again)   

print txt_again.read()   ` 
+0

你遇到什麼錯誤/ s的? – shiva

+0

也許'python.exe'以不同的工作目錄開始。 os.getcwd()'的輸出是什麼? –

+0

dir命令的輸出是什麼? – YOU

回答

0

您可以使用此

import os 
    __location__ = os.path.realpath(
    os.path.join(os.getcwd(), os.path.dirname(__file__))) 

    file_name = raw_input("type the file name") 
    txt = open(os.path.join(__location__, filename)) 
    print txt.read() 
相關問題