Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Python: Popen and os.system woes
New Posts  All Forums:Forum Nav:

Python: Popen and os.system woes

post #1 of 3
Thread Starter 
I have a script that writes timestamps to several files as well as other output. I'm trying to get a Python script to pull out the latest timestamp and save that to a variable but I'm having a rough go of it.

What I do first is iterate through every file in the directory, saving the file name to variable 'file' I was then trying to following:
Code:
command = 'cat {0} | grep [0-24]:[0-59] | tail  -n1'.format(file)
timestamp = os.system(command)

And that would grab the timestamp but also grab the exit code from os.system.
Code:
So it would return:
HH:MM:SS

0 

Instead of 
HH:MM:SS

So then I turned to subprocess.Popen, like so:
Code:
timestamp = subprocess.Popen(command)  # Tried both shell = False and the default of shell = True with no result change

However, this returns a "no such file or directory" error. I'm confused as to why os.system has no issues with the command, but Popen does.

I also attempted:
Code:
timestamp = subprocess.Popen(command.split())
At the advice of someone else. This for some reason is reading '1' into the cat command, causing an error message from cat of "invalid option '1'"


Any help in getting either Popen to function or somehow strip the exit code from the os.system result would be a huge help.
    
CPUMotherboardGraphicsRAM
Core i7 970 @ 4.0 GHz 1.22 Vcore Asus Rampage II Gene GTX 260 216SP G.SKILL PI 3x2gb DDR3 1600 @ 7-8-7-24 
Hard DriveOSMonitorPower
2x 500gb Seagates RAID 0, 1x 500gb non-RAID Windows 7 Professional x64 ASUS 24'' VH242H / Spectre 24'' WS Corsair 750TX 
Case
Corsair 300R 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Core i7 970 @ 4.0 GHz 1.22 Vcore Asus Rampage II Gene GTX 260 216SP G.SKILL PI 3x2gb DDR3 1600 @ 7-8-7-24 
Hard DriveOSMonitorPower
2x 500gb Seagates RAID 0, 1x 500gb non-RAID Windows 7 Professional x64 ASUS 24'' VH242H / Spectre 24'' WS Corsair 750TX 
Case
Corsair 300R 
  hide details  
Reply
post #2 of 3
Try putting your command into a shell script and Popen that.

Also, why are you using cat and grep when Python can do everything those two commands can?
As an example:
Code:
for file in os.listdir(whatever_directory):
    with open(file) as thefile:
        lines = [line for line in thefile]
    for line in lines[::-1]: # Iterating over the list backwards
        if ':' in line:
            print line # Or whatever you're doing with it
            break # Exit the loop since we found the last timestamp

Edited by Ferrari8608 - 5/15/13 at 10:34am
post #3 of 3
Thread Starter 
Quote:
Originally Posted by Ferrari8608 View Post

Try putting your command into a shell script and Popen that.

Also, why are you using cat and grep when Python can do everything those two commands can?
As an example:
Code:
for file in os.listdir(whatever_directory):
    with open(file) as thefile:
        lines = [line for line in thefile]
    for line in lines[::-1]: # Iterating over the list backwards
        if ':' in line:
            print line # Or whatever you're doing with it
            break # Exit the loop since we found the last timestamp


That'll do just swell! Interweb cookies for you, sir.
    
CPUMotherboardGraphicsRAM
Core i7 970 @ 4.0 GHz 1.22 Vcore Asus Rampage II Gene GTX 260 216SP G.SKILL PI 3x2gb DDR3 1600 @ 7-8-7-24 
Hard DriveOSMonitorPower
2x 500gb Seagates RAID 0, 1x 500gb non-RAID Windows 7 Professional x64 ASUS 24'' VH242H / Spectre 24'' WS Corsair 750TX 
Case
Corsair 300R 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Core i7 970 @ 4.0 GHz 1.22 Vcore Asus Rampage II Gene GTX 260 216SP G.SKILL PI 3x2gb DDR3 1600 @ 7-8-7-24 
Hard DriveOSMonitorPower
2x 500gb Seagates RAID 0, 1x 500gb non-RAID Windows 7 Professional x64 ASUS 24'' VH242H / Spectre 24'' WS Corsair 750TX 
Case
Corsair 300R 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Python: Popen and os.system woes