简体   繁体   中英

Open a text file in gedit

I am trying to use os.system for opening text file as below.

def fileshow():
  cf = tb2.get().replace('\n', '')
  hn = tb6.get().replace('\n', '')
  fc = tb3.get().replace('\n', '')
  ff = cf + "/" + hn + "/" + fc
  os.system("gedit ff")

The problem is that gedit opens a file with name ff instead of taking the path value that is stored in ff . Help is needed thanks in advance.

You have ff inside the double-quotes, so it's interpreted as the literal characters ff , not as a reference to the variable named ff .

You could build the command like this:

os.system("gedit " + ff)

But this is dangerous; it can have unexpected results if the value of ff contains spaces, newlines, semicolons, etc. It is recommended to use the subprocess module instead, which lets you pass the arguments in a list instead of having to construct a string representation of the command you're running.

subprocess.run(["gedit", ff])

I think you are forgetting the $ sign to escape the env var.

try:

os.system("gedit $ff")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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