Babbzzz
Babbzzz

Reputation: 194

Python - Opening a File in a Text Editor with Minimal Imports

I'm new to Python. I'm trying to open a file. And, it works.

I understand that we use from sys import argv to import just the function argv from the sys module.

The code I used to open a file is

from sys import argv
import subprocess

script, filename = argv

txt = open (filename)

print "Here's your file %r: " % filename
proc = subprocess.Popen(['scratch-text-editor', 'sample_file'])
proc.wait()

I tried using from subprocess import Popen, but it throws an error.

Here's your file 'sample_file': 
Traceback (most recent call last):
  File "ex12.py", line 9, in <module>
    proc = subprocess.Popen(['scratch-text-editor', 'sample_file'])
NameError: name 'subprocess' is not defined

How can I make it work using from subprocess import Popen. What am I missing here? Isn't this the better way to do it?

Upvotes: 0

Views: 696

Answers (1)

xXliolauXx
xXliolauXx

Reputation: 1313

If you use

from a import b

you can access it just like

b()

and not by doing

a.b()

In your case, just call

Popen() 

Instead of

subprocess.Popen()

Upvotes: 1

Related Questions