简体   繁体   中英

I can't read in a file and write out to a file in Python

import sys 

def main():
    tclust_blue = open("ef_blue.xpk")
    tclust_original = open("tclust.txt","a")

    for line in tclust_blue.readlines():
        if "{}" in line: 
            tclust_original.write(line)

I'm having trouble reading in the file "ef_blue.xpk" which is in the same directory as my Python script as well as the same directory as the tclust.txt file. The error says:

IO Error:[ErrNo 2] No such file or directory: 'ef_blue.xpk'

Also I don't know if my code is correct to begin with. I have a text file called tclust.txt, and I have lines in a file called ef_blue.xpk, and I want to grab values (not lines) from ef_blue.xpk and put them into tclust.txt.

The way I'm executing my file is through terminal by using a command nfxsl readin.py .

Your script uses a relative path, and relative paths are not resolved against "where your script is" but against whatever the current working directory is when the code gets executed. If you don't want to depend on the current working directory (and very obviously you don't), you need to use absolute paths.

Here you have two options: pass the files (or directories...) path as command-line arguments, or use sys.path module's functions and the magic variable __file__ to build an absolute path:

whereami.py:

import os
import sys

print("cwd : {}".format(os.getcwd()))
print("__file__ : {}".format(__file__))
print("abs file : {}".format(os.path.abspath(__file__)))

here = os.path.dirname(os.path.abspath(__file__))
print("parent directory: {}".format(here))

sibling = os.path.join(here, "somefile.ext")
print("sibling with absolute path: {}".format(sibling))

Example output:

bruno@bigb:~/Work$ python playground/whereami.py 
cwd : /home/bruno/Work
__file__ : playground/whereami.py
abs file : /home/bruno/Work/playground/whereami.py
parent directory: /home/bruno/Work/playground
sibling with absolute path: /home/bruno/Work/playground/somefile.ext

As a side note:

First, always close your files - ok the current CPython implementation will close them when your program exits, but that's an implementation detail and not a part of the spec. The simplest way to make sure your files are closed is the with statement:

with open("somefile.ext") as input, open("otherfile.ext", "w") as output:
   # do something with output and input here
   # ...

# at this point (out of the with block), # both files will be closed.

Second point, file.readlines() will read the whole file in memory. If you only want ot iterate once over the lines in your file, just iterate over the file itself, it will avoids eating memory:

 with open("somefile.ext") as f:
     for line in f:
         print f

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