python database ioerror_IOError:[Errno 2]没有这样的文件或目录 - python

我正在尝试在我的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()

而且我真的不理解我的脚本的以下输出:

root@debian:/home/florian/Documents/mysite/polls# python bdd.py

Traceback (most recent call last):

File "bdd.py", line 17, in

with open(file, 'rb') as torrentfile:

IOError: [Errno 2] No such file or directory: 'charlie.torrent'

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

python大神给出的解决方案

您正在尝试打开位于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')。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值