Python - 文件不存在错误 [英] Python - File does not exist error

查看:317
本文介绍了Python - 文件不存在错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用下面的脚本在这里做一些事情(这是不完整的)。第一件事是循环一些子目录。我能够成功地做到这一点。第二件事是打开一个特定的文件(在每个子目录中都是相同的名称),并且在每列EXCEPT中找到最小和最大值。



现在我被困在一列中找到最大值,因为我正在读取的文件有两行,我想忽略。不幸的是,尝试运行代码时,我收到以下错误:

 追溯(最近的最后一次呼叫):
文件test_script.py,第22行,在< module>
打开(文件)为f:
IOError:[Errno 2]没有这样的文件或目录:'tc.out'

以下是我的代码的当前状态:

 导入scipy as sp 
import os

rootdir ='mydir'; #mydir已从实际目录路径更改
data = []

for root,dirs,os.walk(rootdir)中的文件:
文件中的文件:
如果file ==tc.out:
with open(file)as f:
for row in itertools.islice(f,3,None):
for line in文件:
fields = line.split()
rowdata = map(float,fields)
data.extend(rowdata)
print'Maximum:',max(data)


解决方案

当你写 open ),Python正在尝试在您开始解释器的目录中找到文件 tc.out 。您应该使用open(os.path.join(root,file))中的该文件的完整路径:

 作为f:

让我举个例子说明:



我在目录 / tmp / sto / deep / 中有一个名为somefile.txt的文件(这是一个Unix系统,所以我使用正斜杠) 。然后我有这个简单的脚本,它位于目录 / tmp

  oliver @ armstrong:/ tmp $ cat myscript.py 
import os

rootdir ='/ tmp'
为root,dirs,os.walk(rootdir)中的文件:
对于文件中的fname:
如果fname =='somefile.txt':
with open(os.path.join(root,fname))as f:
print ('文件名:%s'%fname)
print('directory:%s'%root)
print(f.read())
pre>

当我从 / tmp 目录中执行此脚本时,您会看到 fname 只是文件名,通向它的路径被省略。这就是为什么你需要从 os.walk 中的第一个返回的参数加入。

  oliver @ armstrong:/ tmp $ python myscript.py 
文件名:somefile.txt
目录:/ tmp / sto / deep
内容


I'm trying to do a couple things here with the script below (it is incomplete). The first thing is to loop through some subdirectories. I was able to do that successfully. The second thing was to open a specific file (it is the same name in each subdirectory) and find the minimum and maximum value in each column EXCEPT the first.

Right now I'm stuck on finding the max value in a single column because the files I'm reading have two rows which I want to ignore. Unfortunately, I'm getting the following error when attempting to run the code:

Traceback (most recent call last):
  File "test_script.py", line 22, in <module>
    with open(file) as f:
IOError: [Errno 2] No such file or directory: 'tc.out'

Here is the current state of my code:

import scipy as sp
import os

rootdir = 'mydir'; #mydir has been changed from the actual directory path
data = []

for root, dirs, files in os.walk(rootdir):
    for file in files:
        if file == "tc.out":
            with open(file) as f:
                for line in itertools.islice(f,3,None):
                    for line in file:
                    fields = line.split()
                    rowdata = map(float, fields)
                    data.extend(rowdata)
                    print 'Maximum: ', max(data)

解决方案

When you write open(file), Python is trying to find the the file tc.out in the directory where you started the interpreter from. You should use the full path to that file in open:

with open(os.path.join(root, file)) as f:

Let me illustrate with an example:

I have a file named 'somefile.txt' in the directory /tmp/sto/deep/ (this is a Unix system, so I use forward slashes). And then I have this simple script which resides in the directory /tmp:

oliver@armstrong:/tmp$ cat myscript.py
import os

rootdir = '/tmp'
for root, dirs, files in os.walk(rootdir):
    for fname in files:
        if fname == 'somefile.txt':
            with open(os.path.join(root, fname)) as f:
                print('Filename: %s' % fname)
                print('directory: %s' % root)
                print(f.read())

When I execute this script from the /tmp directory, you'll see that fname is just the filename, the path leading to it is ommitted. That's why you need to join it with the first returned argument from os.walk.

oliver@armstrong:/tmp$ python myscript.py
Filename: somefile.txt
directory: /tmp/sto/deep
contents

这篇关于Python - 文件不存在错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆