0

is there a way to open files without using QFileDialog.getOpenFileName parameter? 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. 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 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()
tshepang
  • 12,111
  • 21
  • 91
  • 136
user1871869
  • 3,317
  • 13
  • 56
  • 106

1 Answers1

0

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:

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.

you can use the webbrowser module too, though not supported use of it I guess:

import webbrowser
webbrowser.open(self.filename)
MadeOfAir
  • 2,933
  • 5
  • 31
  • 39