简体   繁体   中英

Why I can't open and read the file on python in powershell?

I'm pretty sure I got the ex15_sample.txt in mystuff filefolder!

Here is the error I encounter: Powershell

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

That's my code

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()          `

you can use this

    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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM