0

As far as you know, we can use OS console commands, (For example dir,time and format in Windows) in Python programing using os.system('TheCommand') module. But this function return the state of Operation (0 for successful and 1 for failed).

I want to know if is there any way to use the output of the commands in the next commands? I mean (For example) I run os.system('dir') and save the list of directories in a variable!

TheGoodUser
  • 1,188
  • 4
  • 26
  • 52
  • 5
    Why don't you use `os.listdir` instead of making a console call? – jonrsharpe Sep 05 '14 at 16:15
  • Might also be worth nothing that system calls should probably be made with the newer `subprocess` module unless supporting backward compatible versions of python. –  Sep 05 '14 at 16:19
  • @jonrsharpe The `dir` was just an example. I want to use all another commands too – TheGoodUser Sep 05 '14 at 16:20
  • In that case, @alexwlchan’s comment is correct. I’ll close this question then. Just remember to check if there might not be library functions that already do the job, since you stay platform-independent that way. – poke Sep 05 '14 at 16:21
  • Something like http://stackoverflow.com/questions/4760215/running-shell-command-from-python-and-capturing-the-output? – Aran-Fey Sep 05 '14 at 16:28

1 Answers1

-1

This is fairly easy to do. Here I define the working directory and the edit time of a file as variables which I used later in my script.

#!/usr/bin/env python

PWD = os.getcwd()
edit_time=os.path.getmtime(file.txt)
PhysicalChemist
  • 540
  • 4
  • 14