简体   繁体   English

打开没有getOpenFileName的文件?

[英]Open a file without getOpenFileName?

is there a way to open files without using QFileDialog.getOpenFileName parameter? 有没有不使用QFileDialog.getOpenFileName参数打开文件的方法? The thing is, I have a some buttons that upon clicking them, a notepad will pop up in which you can type anything into the notepad. 关键是,我有一些按钮,单击它们会弹出一个记事本,您可以在其中输入任何内容。 Then, you can save whatever you wrote in that notepad as a text file. 然后,您可以将在该记事本中编写的任何内容保存为文本文件。 What I want to do is, if I click the button again, I will reopen the file that I had previously edited via the notepad and can continue typing where I left off. 我想做的是,如果再次单击该按钮,我将重新打开以前通过记事本编辑的文件,并可以继续输入我从上次中断的地方。 However, I don't want to use getOpenFileName . 但是,我不想使用getOpenFileName Would it be possible to open a file without using this functionality? 不使用此功能就可以打开文件吗? Below is my attempt but my if statement keeps evaluating to be false. 下面是我的尝试,但是我的if语句不断评估为假。 If anyone could help that would be great. 如果有人可以帮助,那就太好了。 Thanks! 谢谢!

    #Testing if the file already exists
    if(os.path.exists("~/Desktop/" +self.fileName + ".txt")):
        f = open(self.fileName + ".txt", 'r')
        filedata = f.read()
        self.text.setText(filedata)
        f.close()
    #Opens a new notepad if there wasn't a previous fileconstructed
    else:
        self.textBox = textBoxWindow(self.fileName)
        self.textBox.show()

If you are on Winsows (you said the word Notepad ), you can use the subprocess module to open any file with whatever program currently associated with the file type as follows: 如果您使用的是Winsows(您说的是Notepad ),则可以使用subprocess进程模块打开具有当前与该文件类型关联的程序的任何文件,如下所示:

import subprocess

self.filename = r'C:\test.txt'
subprocess.call(['start', self.filename], shell=True)

But the shell=True argument is kinda dangerous, especially of the filename comes as an input. 但是shell=True参数有点危险,尤其是文件名作为输入。

you can use the webbrowser module too, though not supported use of it I guess: 您也可以使用webbrowser模块,尽管我不支持使用它:

import webbrowser
webbrowser.open(self.filename)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM