gpt4 book ai didi

python - 如何使用 python 挂载 USB 设备或硬盘分区

转载 作者:太空宇宙 更新时间:2023-11-04 04:58:37 24 4
gpt4 key购买 nike

我一直在尝试编写一个程序,将设备安装在指定位置,一切都将由用户输入完成。

我使用了ctypes。我卡住的地方就在这部分

 def mount(source, target, fs, options=''):
ret = ctypes.CDLL('libc.so.6', use_errno=True).mount(source, target, fs, 0, options)
if ret < 0:
errno = ctypes.get_errno()
raise RuntimeError("Error mounting {} ({}) on {} : {}".
format(source, fs, target, os.strerror(errno)))

我收到一条错误消息,提示“参数无效”,位于

挂载(a, b, 'ntfs', ' -w')

以下是我的整个代码:

import os
import ctypes

print "Usb device management"

def mount(source, target, fs, options=''):
ret = ctypes.CDLL('libc.so.6', use_errno=True).mount(source, target, fs, 0, options)
if ret < 0:
errno = ctypes.get_errno()
raise RuntimeError("Error mounting {} ({}) on {} : {}".
format(source, fs, target, os.strerror(errno)))

def umount(source):
retu = ctypes.CDLL('libc.so.6', use_errno = True).umount(source)
if retu < 0:
errno1 = ctypes.get_errno1()
raise RuntimeError("error unmounting {} ".
format(source))


while True :
print "\n 1. Mount \n 2. Unmount \n 3. Write to file \n 4. Read File \n 5. Copy \n 6. Exit"
choice = int(raw_input('Enter the choice : '))

if choice == 1:
a = raw_input('Enter device name ')
b = raw_input('Enter mount location ')
mount(a, b, 'ntfs', ' -w')
print "USB mounted"

elif choice == 2:
print "Unmounting USB device"
c=raw_input('Enter USB device location ')
umount (c)
print "USB device unmounted"
elif choice == 3:
string = raw_input('Give input to write to file')
fd = open("%s/file.txt"%(b), 'w')
fd.write('string')
print "file Write successfull"
fd.close()
elif choice == 4:
print "Reading file"
fd = open("%s/file.txt"%(b),'r')
print "File read successfull "
fd.close()
elif choice == 5:
copy_location = raw_input('Enter location to where it has to be copied')
print "Copying file "
os.system("cp %s/file.txt %s"%(b, copy_location))
print "%s %s"%(b, copy_location)
print("File copied to location $s "%(copylocation))
if choice == 6:
print "Exit bye"
break;

我的系统是 Ubuntu 15.10。

最佳答案

如果您查看man 2 mount,您会发现安装标志必须是数字,而不是字符串。这是一个明显的错误;我不能说这是否是你所有的错误。

关于python - 如何使用 python 挂载 USB 设备或硬盘分区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39477682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com