user9917517
user9917517

Reputation:

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.

Upvotes: 0

Views: 589

Answers (2)

Chris Martin
Chris Martin

Reputation: 30736

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])

Upvotes: 2

l p
l p

Reputation: 528

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

try:

os.system("gedit $ff")

Upvotes: 0

Related Questions