0

I am a newbie to python and linux. I want a solution for listing the files and folders based on the timestamp. I know this is already asked. But I cannot get an insight in what I am doing. I want the code to return the latest created file or folder. I have a script that identifies the type of content(file or folder). I need it to get the latest created content. The content identifier goes like this.

import os
dirlist=[]
dirlist2=[]
for filename in os.listdir('/var/www/html/secure_downloads'):
    if (os.path.isdir(os.path.join('/var/www/html/secure_downloads',filename))):
        dirlist.append(filename)
    else:
       dirlist2.append(filename)
print "For Folders",dirlist 
print "For Files",dirlist2

1 Answer 1

1

Option 1: Use glob

I found this great article here: https://janakiev.com/blog/python-filesystem-analysis/

Option 2: Pipe the output of ls -l to python

The way I initially thought about solving this issue is doing the following...

You can list all the directories and their timestamps with ls -l.

Then you can pipe that output using subprocess like this:

import subprocess
proc=subprocess.Popen('echo "to stdout"', shell=True, stdout=subprocess.PIPE, )
output=proc.communicate()[0]
print(output)
Sign up to request clarification or add additional context in comments.

1 Comment

I mean, I can't understand the option 2. Could you explain step by step. Sorry I am total noob.

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.