Reputation: 27
I'm trying to pull a filename from the command line using OS in python. I'm searching for a file that's created at a specific time. While I can get the result I want, I can't do anything with what I get. Here' my code:
import os
file = str(os.system('ls -ltr | grep fileName | grep 09:29 | head -1'))
The result is:
-rw-r--r-- l dir dir 126649 Jun 14 09:29 fileName.011
However, when I try to strip out just the file name, I just get the whole line.
I've tried using file[-11:]
without success.
I've confirmed the type of the result is a string:
<type 'str'>
I've tried adding the result to a list and then splitting it, but that didn't work either.
Thank you for the help.
Upvotes: 0
Views: 58
Reputation: 3115
os.system()
does not return the output from the shell.
Use subprocess.check_output()
for that
import subprocess
output = subprocess.check_output('ls -ltr | grep fileName | grep 09:29 | head -1', shell=True))
And then you can slice your filename from output
.
Upvotes: 2