标签: ioerror

为什么我在Python上得到不一致的异常?

我在Python中遇到了一个非常奇怪的行为,这种行为不一致.

...
except IOError as msg:
    sys.exit("###ERROR IOError: %s" % (msg))
Run Code Online (Sandbox Code Playgroud)

通常这会给我一个消息,如:

###ERROR IOError: [Errno 13] Permission denied: 'filename'
Run Code Online (Sandbox Code Playgroud)

在相同的情况下,上面的代码给了我一个tuple而不是一个正确的错误消息.

###ERROR IOError: (13, 'Permission denied')
Run Code Online (Sandbox Code Playgroud)

这很奇怪,因为在所有情况下异常都来自同一个python方法, codecs.open(...)

是什么让我想知道更多关于这一点,如果我删除处理异常将使用正确的文本(完整的错误消息)到达上层,总是!

except IOError as msg:
    print(msg)
    raise msg
Run Code Online (Sandbox Code Playgroud)

以上示例将始终打印完整的消息,例如IOError: [Errno 13] Permission denied: u'filename'.

为什么会发生这种情况,如何防止这种情况发生,我不想向用户提供不完整的错误消息.

我想在测试文件中重现此行为,但我无法在项目外重现此问题.

我怀疑这与使用有关,sys.exit()因为print(msg)会给出好结果但sys.exit不会.

python exception-handling ioerror

3
推荐指数
1
解决办法
418
查看次数

Django 1.6 IOError

我已将我的django版本从1.5.2升级(如果我不记得不好)到1.6,现在当我在localhost上执行我的项目时,我收到此错误:

IOError at /
[Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/httplib2-0.8-py2.7.egg/EGG-INFO/top_level.txt'
Request Method: GET
Request URL:    http://localhost:8000/
Django Version: 1.6.1
Exception Type: IOError
Exception Value:    
[Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/httplib2-0.8-py2.7.egg/EGG-INFO/top_level.txt'
Exception Location: /usr/local/lib/python2.7/dist-packages/pkg_resources.py in _get, line 1320
Python Executable:  /usr/bin/python
Python Version: 2.7.5
Run Code Online (Sandbox Code Playgroud)

django ioerror

2
推荐指数
1
解决办法
393
查看次数

IOError:[Errno 2]没有这样的文件或目录

我试图在我的MySQL数据库的表的路径中添加所有种子文件的一些信息,但似乎我有一些PATH问题.你可以看到有完整的路径,它甚至检测到"charlie.torrent",所以我真的不明白是什么问题.

这是我的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import mysql.connector
import bencode
import binascii
import hashlib
import os
import sys

conn = mysql.connector.connect(host="localhost",user="root",password="root", database="TORRENTS")
cursor = conn.cursor
path = "/home/florian/TorrentFiles"
dirs = os.listdir(path)
for file in dirs:
        try:
                with open(file, 'rb') as torrentfile:
                        torrent = bencode.bdecode(torrentfile.read())
                        user = ("torrent['info']['name']","torrent['info']['length'],'bytes'","(hashlib.sha1(bencode.bencode(torrent['info'])).hexdigest())")
                        cursor.execute("""INSERT INTO torrent_infos (Name, Size, Hash) VALUES(%s, %s, %s)""", user)
        except bencode.BTL.BTFailure:
                continue


conn.close()
Run Code Online (Sandbox Code Playgroud)

我真的不明白我的脚本的以下输出:

root@debian:/home/florian/Documents/mysite/polls# python bdd.py 
Traceback (most recent call last):
  File "bdd.py", line 17, in <module>
    with …
Run Code Online (Sandbox Code Playgroud)

python path ioerror python-2.7

2
推荐指数
1
解决办法
3050
查看次数

奇怪的异常行为

我想实现一个异常显示器,它不起作用,但我有一些不同的东西:

Traceback (most recent call last):
    File "/Users/honzik/PycharmProjects/Toy/test.py", line 21, in main
    raise IOError('Foo bar bazooka!')
OSError: Foo bar bazooka!
Run Code Online (Sandbox Code Playgroud)

当我指定IOError时如何引发OSError?

python exception ioerror python-3.5

2
推荐指数
1
解决办法
36
查看次数

Windows上的Python:IOError:[Errno 2]没有这样的文件或目录

首先,我对Python和编程很新.

目前我正在尝试创建一个脚本,根据黑名单中的行删除文件夹中随机名称,扩展名和内容的所有文件(搜索必须在文件内容中完成).

这是一个代码:

import os

black_list = [line for line in open("C:/path/to/blacklist.txt")]

for filename in os.listdir("C:/path/to/files/"):
    content = open(filename).read()
    if any(line in content for line in black_list):
        os.remove(filename)
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

IOError:[Errno 2]没有这样的文件或目录:'first_file_from_the_folder'

请问你能帮帮我吗?

提前致谢!

python ioerror

2
推荐指数
1
解决办法
5144
查看次数

尝试打开现有文件时出现IOError

我有一个小问题,我编写的python程序是从一个特殊的文本文件中提取一些信息.循环(下面的代码)需要执行我的函数extract_zcoords()超过500个文件(1个文件给出一个列表),以便我可以构建数据集.

import os

def extract_zcoord(filename): 
    f = open(filename, 'r')         
    ... # do something with f


### LOOP OVER DIRECTORY 
location = '/Users/spyros/Desktop/3NY8MODELSHUMAN/HomologyModels' 

for filename in os.listdir(location):
    extract_zcoord(filename) 
Run Code Online (Sandbox Code Playgroud)

错误:IOException No such file or directory是发生的,因此由于某种原因python不访问文件.我检查了目录路径名(位置)和文件权限,它们是正确的(读取+写入).当文件存在且路径名是否正确时,为什么会报告IOError的任何想法?

什么想法可能是错的?

python file-io fileparsing ioerror

1
推荐指数
1
解决办法
2224
查看次数

try上的缩进错误,除了python中的else块

对于我在这里问的一个问题,我很难解决错误异常.我已经整理出从另一个文件中读取文件列表,但是如果引用的其中一个文件不存在,我就会挣扎.我希望能够识别错误,发送电子邮件,然后创建该文件供以后使用.但是,我正在为"我正在尝试的"尝试,除了,"块"收到缩进错误.它看起来应该可以正常工作,但我无法让它运行!尔加!救命!!!

日志文本文件包含以下内容:

//server-1/data/instances/devapp/log/audit.log

//server-1/data/instances/devapp/log/bizman.db.log

//server-1/data/instances/devapp/log/foo.txt# 服务器上不存在此文件

我的代码如下.我认为最好将它全部发布而不是一个片段,以防它在程序中更早出现它的东西!

import os, datetime, smtplib
today = datetime.datetime.now().strftime('%Y-%m-%d')
time_a = datetime.datetime.now().strftime('%Y%m%d %H-%M-%S')
checkdir = '/cygdrive/c/bob/python_'+ datetime.datetime.now().strftime('%Y-%m-%d')+'_test'
logdir = '/cygdrive/c/bob/logs.txt'
errors = '/cygdrive/c/bob/errors.txt'

#email stuff
sender = 'errors@company.com'
receivers = 'bob@company.com'
message_1 = """From: errors <errors@company.com>
To: Bob <bob@company.com>
Subject: Log file not found on server

A log file has not been found for the automated check. 
The file has now been created.
""" 
#end of email stuff


try:
                os.chdir (checkdir) # Try …
Run Code Online (Sandbox Code Playgroud)

python indentation ioerror

1
推荐指数
1
解决办法
8405
查看次数

Python IOError 虽然有很多但无法分配内存

我编写了一个基本程序来检查包含许多 jpeg 文件(500000+)的目录树,验证它们没有损坏(大约 3-5% 的文件似乎以某种方式损坏),然后对文件(即使是损坏的文件)并将信息保存到数据库中。

有问题的 jpeg 文件位于 windows 系统上,并通过 cifs 安装在 linux box 上。它们的大小大多在 4 兆字节左右,尽管有些可能稍大或稍小。

当我运行该程序时,它似乎工作得很好一段时间,然后它因以下错误而失败。这是在它处理了大约 1100 个文件之后(错误表明问题发生在尝试打开 4.5 兆的文件时)。

现在我明白我可以捕获这个错误并继续或重试等,但我很好奇为什么它首先发生,如果捕获和重试实际上可以解决问题 - 或者它会卡在重试中(除非我当然限制重试但随后会跳过一个文件)。

我在 debian 系统上使用“Python 2.7.5+”来运行它。系统至少有 4 Gig(可能是 8 个)的 ram,并且 top 报告脚本在运行时的任何时候都使用不到 1% 的 ram 和不到 3% 的 cpu。同样,此脚本运行的 jpeginfo 也使用同样少量的内存和 CPU。

为了避免在读取文件时使用太多内存,我采用了本回答中给出的另一个问题的方法:https : //stackoverflow.com/a/1131255/289545

此外,您可能会注意到“jpeginfo”命令在寻找“[OK]”响应的 while 循环中。这是因为如果“jpeginfo”认为它找不到文件,它会返回 0,因此 subprocess.check_output 调用不会将其视为错误状态。

我确实想知道 jpeginfo 在第一次尝试时似乎无法找到某些文件的事实是否可能相关(我怀疑是这样),但返回的错误表示无法分配内存而不是文件未找到。

错误:

Traceback (most recent call last):
  File "/home/m3z/jpeg_tester", line 95, in <module>
    main()
  File "/home/m3z/jpeg_tester", line 32, in __init__
    self.recurse(self.args.dir, self.scan) …
Run Code Online (Sandbox Code Playgroud)

python ioerror

1
推荐指数
1
解决办法
4398
查看次数

FileNotFoundException:/ storage/emulated/0/Android

我尝试这个文件编写器/阅读器代码段进行测试:

File file = new File(Environment.getExternalStorageDirectory(), "LM/lm_lisdat_01.txt");
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(("test").getBytes());
outputStream.close();

File file = new File(getExternalFilesDir(null), "LM/lm_lisdat_01.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
Run Code Online (Sandbox Code Playgroud)

在4.行中我收到此错误消息,但在LM目录中创建了"lm_lisdat_01.txt"文件:

java.io.FileNotFoundException:/storage/emulated/0/Android/data/hu.abisoft.lm/files/LM/lm_lisdat_01.txt:open failed:ENOENT(没有这样的文件或目录)

可以帮助任何人回答这个(我认为简单)的问题吗?我是Android的新手.谢谢!

android file ioerror

1
推荐指数
1
解决办法
2万
查看次数

Python 中的 time.sleep() 时出现奇怪的 IOError

所以在 Python 中使用 time.sleep() 时我有一个非常奇怪的错误。

start = time.time()
# some code goes here
end = time.time()
spent = end - start
time.sleep(1.0101 - spent) # this gives a strange IOError...
Run Code Online (Sandbox Code Playgroud)

我可以修吗?我无法更改已用或 1.0101。
编辑:错误是:IOError: [Errno 22] 无效参数。
EDIT2:我使用的是 Raspberry Pi 2。

python ioerror

1
推荐指数
1
解决办法
2756
查看次数