简体   繁体   中英

use Python to open a file in read mode

I am trying to open a file in read mode using Python. The error I am receiving suggest I am using the won filename or read mode. When I type the file path into my computer, it works. I tried to assign the input filename to a variable and then opening the variable in read mode. I also tried typing in the full path and opening the path in read mode. Both game me an error.

Code:

workingDirec = raw_input("What is the working directory?")
original_file = raw_input("The input filename is?")

def calculateZscore():
    "Z score calc"
full_original = os.path.join(workingDirec,original_file)
print full_original

f = open ('C:\Users\tpmorris\ProgramingAndScripting\Trial 2 Data\Trial 2 Data\NCSIDS_ObsExp.txt','r')
print f

My results:

Using full path output:

What is the working directory?C:\\Users\\tpmorris\\ProgramingAndScripting\\Trial 2 Data\\Trial 2 Data

The input filename is?NCSIDS_ObsExp.txt

C:\\Users\\tpmorris\\ProgramingAndScripting\\Trial 2 Data\\Trial 2 Data\\NCSIDS_ObsExp.txt

IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Users\\tpmorris\\ProgramingAndScripting\\Trial 2 Data\\Trial 2 Data\\NCSIDS_ObsExp.txt'

Using variable output:

IOError: [Errno 2] No such file or directory: 'full_original'

On Windows your paths must be escaped because Windows uses backslashes \\ to denote path separators.

Backslashes however are typically used as escape sequences and are used in Python as such as well! So you have to "escape" them like this:

f = open ('C:\\Users\\tpmorris\\ProgramingAndScripting\\Trial 2 Data\\Trial 2 Data\\NCSIDS_ObsExp.txt','r')

See:

Firstly , on Windows, you must escape backslashes (double backslash) if you are going to use Windows path syntax, for the reasons pointed out by @James Mills answer.

Another option is to use forward slashes; Python will interpret these correctly in os.path.

You could use as your command line path input:

C:/Users/tpmorris/ProgramingAndScripting/Trial 2 Data/Trial 2 Data

Or add

/NCSIDS_ObsExp.txt

to the above if you were going to use a hardcoded path.

Also you should make some small changes to your code if you want to print the contents of your text file:

First, your file open should be done using a with statement. This will ensure the file object's built in __enter__ and __exit__ methods get called, in particular, if you forget to close the file when you are done after you've opened it.

See Understanding Python's with statement for more.

Second, if you want to print each line in your text file, don't try to print the file object. Rather loop through the lines and print them.

So your code for accepting command line input should be:

    import os

    workingDirec = raw_input("What is the working directory?")
    original_file = raw_input("The input filename is?")


    full_original = os.path.join(workingDirec,original_file)
    print full_original


    with open(full_original,'r') as f:
        for line in f:
            print line
    f.close()

I removed the def of a function to do something else in the midst of your file read code. That def should go elsewhere.

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