IOError:[Errno 2]没有这样的文件或目录 [英] IOError: [Errno 2] No such file or directory
问题描述
我正在尝试在我的MySQL数据库的表的路径中添加所有种子文件的某些信息,但似乎我遇到了一些PATH问题. 如您所见,这里有完整的路径,它甚至可以检测到"charlie.torrent",所以我不太了解问题所在.
I'm trying to add some informations of all the torrents file in a path to a Table of my MySQL database but it seems like i have some PATH problems. As you can see there is the full path and it even detect the "charlie.torrent" so i don't really understand what is the problem.
这是我的代码:
#!/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()
我真的不理解我的脚本的以下输出:
And i really don't understand the following output of my script:
root@debian:/home/florian/Documents/mysite/polls# python bdd.py
Traceback (most recent call last):
File "bdd.py", line 17, in <module>
with open(file, 'rb') as torrentfile:
IOError: [Errno 2] No such file or directory: 'charlie.torrent'
我已经看过其他相同的主题,没有任何结果.
推荐答案
您正在尝试打开位于path
中的文件,但不包括该路径,该路径会尝试在您当前的工作路径中打开该文件Python脚本.例如,如果您的种子在/home/user/torrents
中,则从/home/user/script.py
运行脚本.当您执行open(file, 'rb')
时,您正在执行/home/user/charlie.torrent
,而不是/home/user/torrents/charlie.torrent
.尝试用with open(os.path.join(path, file), 'rb')
替换with open(file, 'rb')
.
You're trying to open a file located in path
, but not including that path, which tries to open the file in the current working path of your Python script. For example, if you run the script from /home/user/script.py
, while your torrents are in /home/user/torrents
. When you do open(file, 'rb')
you are doing /home/user/charlie.torrent
as opposed to /home/user/torrents/charlie.torrent
. Try replacing with open(file, 'rb')
with with open(os.path.join(path, file), 'rb')
.
这篇关于IOError:[Errno 2]没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!