3 Replies - 2656 Views - Last Post: 11 March 2011 - 09:55 AM Rate Topic: -----

#1 Igneus  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 17
  • Joined: 27-December 09

Python os.popen() not working as expected

Posted 10 March 2011 - 11:25 PM

Hey, I've been fooling around with making some keyboard macros under Ubuntu on my laptop. Although, now I'm getting tired of the simple "Launch Internet" stuff, and thought it would be fun to program some slightly more complicated ones. So to start, I thought I would write a program that generates a GUI and asks for a filename. Once provided, the user would click the "Launch" button and it would open that file in Emacs. Well, I've gotten the program to load Emacs, but I don't believe that os.popen() is working the way it is suppose to, or, I'm expecting it to do more than what it actually can.

Below is all the code for this program, it's very small and simple currently:

from Tkinter import *
import os

class EmacsLauncher(Frame):

    def __init__(self):
        """Set up Graphical User Interface Frame."""
        Frame.__init__(self)
        self.master.title("Emacs Launcher")
        self.grid()
        self._label1 = Label(self, text = "File:")
        self._label1.grid(row = 0, column = 0)
        self._fileTextVar = StringVar()
        self._fileText = Entry(self, textvariable = self._fileTextVar)
        self._fileText.grid(row = 0, column = 1)
        self._launchButton = Button(self, text = "Launch", command = self._launch)
        self._launchButton.grid(row = 1, column = 1)

    def _launch(self):
        """Open the _fileTextVar file in Emacs"""
        filePath = "emacs " + str(self._fileTextVar)
        os.popen(filePath, "w")

def main():
    """Run main loop"""
    EmacsLauncher().mainloop()

main()



Basically all the code is just setting up the GUI, and the main point of interest are these lines below:

filePath = "emacs " + str(self._fileTextVar)
os.popen(filePath, "w")



What happens when I run this code is that the program with open Emacs as if you entered just "emacs" in the terminal. What I was under the impression that os.popen() should do is open Emacs, but with the filename. For instance, if one was to enter "emacs example.py" into the terminal. I've browsed the link below as it contains documentation of the os.popen() function, however, I just can't seem to make heads or tails of it.


Python v2.7.1 Documentaion


If anyone has some insight for me, it would be greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Python os.popen() not working as expected

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 797
  • Joined: 08-June 10

Re: Python os.popen() not working as expected

Posted 11 March 2011 - 06:03 AM

I would suggest moving to subprocess.Popen() or subprocess.call() because, rather than passing a full execution string--which can be unreliable and insecure--you pass a list of shell arguments. For instance:

#! /usr/bin/env python
import subprocess

p = subprocess.Popen(['ls', '-la', '/home'])



#! /usr/bin/env python
import subprocess

p = subprocess.call(['emacs', 'example.py'])



When passing user input to shells or allowing users to control execution, you will need to ensure that you are striping out any dangerous activities. For instance, in your example, if a user had entered "example.py ; rm -rf ~" (and your program actually worked), you would have found yourself missing your home directory!

This post has been edited by Motoma: 11 March 2011 - 06:03 AM

Was This Post Helpful? 3
  • +
  • -

#3 Igneus  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 17
  • Joined: 27-December 09

Re: Python os.popen() not working as expected

Posted 11 March 2011 - 07:46 AM

Brilliant! Thank you very much. And yes, it would be very horrific if I were to lose my home folder lol. Thank you for the help though! I will definitely try that after my classes today.
Was This Post Helpful? 0
  • +
  • -

#4 Igneus  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 17
  • Joined: 27-December 09

Re: Python os.popen() not working as expected

Posted 11 March 2011 - 09:55 AM

Well, Popen() works. Turns out a made a pretty simple error in my code:

p = subprocess.Popen(['emacs', str(self._textFileVar)])



Should have been:

p = subprocess.Popen(['emacs', str(self._textFileVar.get())])



But thank you for you help, glad you informed me about the subprocess.Popen().
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1