(PyUSB)python 代码/文件无法正确打开和读取 USB 上的 txt 文件(代码问题)
python 代码只列出来自“运行文件”同一目录的文件,我想读取 txt 文件 USB
我使用的是 Linux。我知道我的 USB 名称是:
Bus 001 Device 005: ID 8564:1000 Transcend Information, Inc. JetFlash
idVendor 和 idProduct 是:
idVendor 0x8564 Transcend Information, Inc. idProduct 0x1000 JetFlash
运行文件代码为:
import usb.core
import usb.util
import os
dev = usb.core.find(idVendor= 0x8564, idProduct=0x1000)
ep = dev[0].interfaces()[0].endpoints()[0]
i=dev[0].interfaces()[0].bInterfaceNumber
dev.reset()
if dev.is_kernel_driver_active(i):
print("hello")
current_directory = os.getcwd()
print(current_directory) # => /home/borislav/Desktop/bobbyhadz_python
contents = os.listdir(current_directory)
print(contents) #
答案1
主要思想是使用 np.loadtxt
和了解 txt 文件路径
data_inside_content2222222 = np.loadtxt("/media/joy/Data/1111.txt", dtype='str' , delimiter=",") 打印(data_inside_content2222222)
USB txt文件的路径:
/media/joy/Data/1111.txt”
- 已解决的代码
import usb.core
import usb.util
import os
import numpy as np
dev = usb.core.find(idVendor= 0x8564, idProduct=0x1000)
ep = dev[0].interfaces()[0].endpoints()[0]
i=dev[0].interfaces()[0].bInterfaceNumber
dev.reset()
if dev.is_kernel_driver_active(i):
print("hello joy")
data_inside_content2222222 = np.loadtxt("/media/joy/Data/1111.txt", dtype='str' , delimiter=",")
print(data_inside_content2222222)
print("= = = = = = = = = = ")
current_directory = os.getcwd()
#