0

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?

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly! Thank you. If someone can think of a more apt question title; please change it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.