Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 有没有一种方法可以指定并用Python打开一个文件?_Python 3.x_Tkinter_Filesystems - Fatal编程技术网

Python 3.x 有没有一种方法可以指定并用Python打开一个文件?

Python 3.x 有没有一种方法可以指定并用Python打开一个文件?,python-3.x,tkinter,filesystems,Python 3.x,Tkinter,Filesystems,我正在尝试制作一个程序,允许用户从他们的计算机中选择一个文件,然后打开它。我一直在尝试用Python实现这一点,而且 filedialog.askopenfilenames() …使用这个Tkinter小部件 我可以成功地从中获取文件路径,但是如何使用它来实际打开文件呢?(我试图在它的默认应用程序中打开它,而不仅仅是将它打印到Python控制台) from subprocess import call call(['xdg-open','filename']) 用“files”(存储文件名的

我正在尝试制作一个程序,允许用户从他们的计算机中选择一个文件,然后打开它。我一直在尝试用Python实现这一点,而且

filedialog.askopenfilenames()
…使用这个Tkinter小部件

我可以成功地从中获取文件路径,但是如何使用它来实际打开文件呢?(我试图在它的默认应用程序中打开它,而不仅仅是将它打印到Python控制台)

from subprocess import call
call(['xdg-open','filename'])
用“files”(存储文件名的变量)替换“filename”,但我得到以下错误:

Traceback (most recent call last):
  File "/Users/charlierubinstein/Documents/Search Engine.py", line 9, in <module>
    call(['xdg-open', files])
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 267, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1275, in _execute_child
    restore_signals, start_new_session, preexec_fn)
TypeError: expected str, bytes or os.PathLike object, not tuple

如前所述,理想情况下,此程序将允许用户选择一个文件,然后在其默认应用程序中打开该文件。

您使用
askopenfilenames()
(名称末尾带有s

它允许您选择多个文件,以便返回所有选定文件的元组-即使您只选择了一个文件
(如果取消选择,则为空元组)

所以必须从元组中获取第一个元素

call(['xdg-open', files[0] ])  

或者使用
askopenfilename()
(名称末尾不带s),您将得到一个字符串

filename = filedialog.askopenfilename() # without `s` at the end

call(['xdg-open', filename])  
使用
打印(文件)
查看错误
filename = filedialog.askopenfilename() # without `s` at the end

call(['xdg-open', filename])