我试着把所有torrents文件的一些信息添加到MySQL数据库的一个表的路径中,但似乎我有一些路径问题。 正如你所看到的那样,它甚至可以检测到查理。托伦特“所以我真的不明白问题出在哪里。在
这是我的代码:
#!/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()
我真的不明白我的脚本的以下输出:
^{pr2}$我已经看过其他相同的实验对象了,但没有任何结果。
您还可以将当前所在的目录更改为path。在
这也应该管用。在
您试图打开位于}。尝试将
path
中的文件,但不包括该路径,该路径将尝试在Python脚本的当前工作路径中打开该文件。例如,如果您从/home/user/script.py
运行脚本,而您的torrent在/home/user/torrents
中。当你做open(file, 'rb')
时,你在做/home/user/charlie.torrent
,而不是{with open(file, 'rb')
替换为with open(os.path.join(path, file), 'rb')
。在相关问题 更多 >
编程相关推荐