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

Bou*_*h10 2 python path ioerror python-2.7

我试图在我的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 open(file, 'rb') as torrentfile:
IOError: [Errno 2] No such file or directory: 'charlie.torrent'
Run Code Online (Sandbox Code Playgroud)

我已经看过其他相同的主题没有任何结果.

pai*_*ima 5

您正在尝试打开位于path但不包含该路径的文件,该文件尝试在Python脚本的当前工作路径中打开该文件.例如,如果您运行脚本/home/user/script.py,而您的种子在/home/user/torrents.当你这样做时,open(file, 'rb')你正在做/home/user/charlie.torrent而不是/home/user/torrents/charlie.torrent.尝试更换with open(file, 'rb')with open(os.path.join(path, file), 'rb').