OSError:[Errno 13]从USB驱动器执行python脚本时权限被拒绝

时间:2017-05-31 11:06:14

标签: python subprocess

当我尝试从USB驱动器运行Python脚本时出现此错误。我已将当前目录中的所有文件复制到USB记忆棒上,以便Python脚本从另一台Ubuntu计算机上运行。

Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1545, in __call__
  return self.func(*args)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 402, in button_press_event
  FigureCanvasBase.button_press_event(self, x, y, num, dblclick=dblclick, guiEvent=event)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 1909, in button_press_event
  self.callbacks.process(s, mouseevent)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/cbook.py", line 549, in process
  proxy(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/cbook.py", line 416, in __call__
  return mtd(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/widgets.py", line 434, in _update
  self.set_val(val)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/widgets.py", line 448, in set_val
  func(val)
File "/media/mghansar/MG/ktn/UQM_Prototype_V1.03_RD4.py", line 509, in update_slider1
  updateval_slider1(self.mu_a1,self.sigma_a1)
File "/media/mghansar/MG/ktn/UQM_Prototype_V1.03_RD4.py", line 488, in updateval_slider1
  getout1= self.surrogate_call(in5)
File "/media/mghansar/MG/ktn/UQM_Prototype_V1.03_RD4.py", line 431, in surrogate_call
  process = Popen([binary_name, '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
  errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
  raise child_exception
OSError: [Errno 13] Permission denied

当我从Ubuntu 16.10中的当前目录执行程序时,它工作正常。

脚本在抛出上述错误时会尝试读取可执行文件。以下是相关代码

    def surrogate_call(self,inputs, binary_name='./ktn-sgp'):
    '''
    @param inputs pandas.DataFrame (one row per input point)
    '''
    from subprocess import Popen, PIPE
    from StringIO import StringIO

    # call the surrogate compiled from C code
    process = Popen([binary_name, '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
    (stdout_data, stderr_data) = process.communicate(inputs.to_csv(header=False, index=False))
    exit_code = process.wait()

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您的USB驱动器很可能是FAT格式的,FAT不包含unix样式文件权限。

您有两种选择:

1)如果您可以控制系统并决定如何安装USB驱动器,您可以按照:https://askubuntu.com/questions/499275/how-to-set-usb-drive-with-executable-permission安装驱动器。关键部分是挂载选项exec,uid,gid和umask。您需要告知操作系统谁拥有USB驱动器上的文件,因为FAT不包含此信息,并允许从驱动器执行。 Umask = 022将文件的权限设置为rwxr-xr-x启用执行位,因为FAT上也缺少此信息。

2)如果那是不可能的,我能看到的唯一方法是执行python而不是你的脚本。不要直接调用脚本,而是使用usb驱动器上的脚本位置调用/ usr / bin / python作为第一个参数。这不需要执行权限,因为Python将使用读取权限读取文件。

哈努哈利