Fedora Linux Support Community & Resources Center

Go Back   FedoraForum.org > Fedora 17/18 > Using Fedora
FedoraForum Search

Forgot Password? Join Us!

Using Fedora General support for current versions. Ask questions about Fedora and it's software that do not belong in any other forum.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 25th March 2012, 01:56 AM
BrandonAdam Offline
Registered User
 
Join Date: Feb 2012
Location: canada
Posts: 12
linuxfirefox
listing file, modification time, and count to text file---in python via fedora16

trying to list the last modicfication time, file count and all the files in my /etc directory. I'm fairly new to this so I'm running trial and errors and seeing what i can do. this is what I have thus far:

#!/usr/bin/python3
import os.path,time
import datetime
os.walk('/etc')
path ='/etc'

listing = os.listdir(path)
[x[0] for x in os.walk('/etc')]
for infile in listing:
print (infile)

def modification_date(filename):
t = os.path.getmtime(filename)
return datetime.datetime.fromtimestamp(t)

def count_em(valid_path):
x = 0
for root, dirs, files in os.walk(valid_path):
for f in files:
x = x+1
return len(files)
print('There are',x,'files in this directory')
print('created: %s' % time.ctime(os.path.getctime(file))


I keep getting an error message; invalid synax line 28- which is the line after my last line of code...any ideas?
Reply With Quote
  #2  
Old 26th March 2012, 10:17 AM
marriedto51 Offline
Registered User
 
Join Date: Jul 2009
Location: England, UK
Posts: 823
linuxfirefox
Re: listing file, modification time, and count to text file---in python via fedora16

I think the error message is simply because you have a missing close bracket on the last line (three open brackets, but only two close brackets).
Reply With Quote
  #3  
Old 26th March 2012, 10:49 AM
jpollard Offline
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,150
linuxfirefox
Re: listing file, modification time, and count to text file---in python via fedora16

It would help if you put code/scripts in a code.../code brackets too.

Python is space sensitive...
Reply With Quote
  #4  
Old 26th March 2012, 11:36 PM
BrandonAdam Offline
Registered User
 
Join Date: Feb 2012
Location: canada
Posts: 12
linuxfirefox
Re: listing file, modification time, and count to text file---in python via fedora16

ok I've changed what I needed. Thank you for your responses. I was able to get the files to list with the current time as the modification time, I wanted to list this modification time when they were actually last modified. So i obviously screwed the code up in the ctime, right? I rewrote the code as follows and now i'm getting a float error..I've never even heard of this..can someone please help? thanks in advance

#!/usr/bin/python3

import glob
import os.path,time
import datetime
os.walk('/etc')
path = '/etc'
import os, time

listing = os.listdir(path)
[x[0] for x in os.walk('/etc')]
for infile in listing:
print (infile)

dirname = '/etc'
print ('File was last modified:')
print(time.strftime('%m/%d/%Y %I:%M:%S %p'),time.localtime(os.path.getmtime)('/etc'))


def count_em(path):
x=0
for root,dirs,files in os.walk(path):
for f in files:
x = x+1
len (path)

print('There are',x,'files in this directory')
print('created: %s' % time.ctime(os.path.getctime(path)))
Reply With Quote
  #5  
Old 27th March 2012, 04:10 AM
marko's Avatar
marko Online
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 5,490
linuxfirefox
Re: listing file, modification time, and count to text file---in python via fedora16

Quote:
print(time.strftime('%m/%d/%Y %I:%M:%S %p'),time.localtime(os.path.getmtime)('/etc'))
There's a typo there, it should be:
Quote:
print(time.strftime('%m/%d/%Y %I:%M:%S %p'),time.localtime(os.path.getmtime('/etc') )
so just remove the bolded red right parenthesis

There's also some bad code in there but they don't have any side effects, that is you have some no effect code that's in there and does nothing like:

os.walk('/etc') --> being used on a single line and so has no effect

def count_em --> function is never used

But without the proper spacing of the file, there could be other things wrong, you should post the code wrapped like jpollard said

Last edited by marko; 27th March 2012 at 04:19 AM.
Reply With Quote
  #6  
Old 27th March 2012, 06:35 AM
BrandonAdam Offline
Registered User
 
Join Date: Feb 2012
Location: canada
Posts: 12
windows_7firefox
Re: listing file, modification time, and count to text file---in python via fedora16

ok so I got this to work, but it's not doing what I want it to do. I've been trying to create a script so I can see the last modification dates of all my files. I added a script to a file within the /etc directory a few weeks back and I cant seem to remember which sub-directory it was in and what the script contained.( i know it sounds ridiculous) I was thinking if in the future I do something like this I could execute this script to get a file count, mod date, and time for each sub directory. This script should be able to be used anywhere I want, so if I want to use it in my /home directory I can just modify the /ect portion within the script and it should work. The one I currently have here is just listing all the files and giving me the date and amount of files in the directory...


~Sorry for the mess i'm fairly new at fedora and I've been using the forums and books to guide me through the process..

cheers
Brandon
Reply With Quote
  #7  
Old 27th March 2012, 03:43 PM
jpollard Offline
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,150
linuxfirefox
Re: listing file, modification time, and count to text file---in python via fedora16

You can always use the "stat" command...

Or "ls -ltr" (long listing, sort by modification time, reverse the order)
Reply With Quote
  #8  
Old 27th March 2012, 04:47 PM
marko's Avatar
marko Online
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 5,490
linuxfirefox
Re: listing file, modification time, and count to text file---in python via fedora16

Quote:
Originally Posted by BrandonAdam View Post

~Sorry for the mess i'm fairly new at fedora and I've been using the forums and books to guide me through the process..

cheers
Brandon
To post the code so the white space formatting survives, just put the code in, highlight it with the mouse and select the "#" symbol from the editor menu (when in Advanced edit mode). That will wrap it in 'code' tags so it's formatted.
Quote:
Originally Posted by BrandonAdam
I cant seem to remember which sub-directory it was in
Also you can find a file anywhere on your filesystem with:
Quote:
locate filename
(just substitute the name of your file for where I have 'filename'). There may be a lot of false positives since locate matches the argument to any string in the entire path but you can reduce that a bit with the -b(asename) option so it only matches to the end of the path:
Quote:
locate -b filename
Also, this might help (python and modification times):
http://stackoverflow.com/questions/2...imes-in-python


UPDATE: by looking at the stackoverflow postings, I came up with a basic script that does what you
seem to want:
Code:
#!/usr/bin/python3
#
# mod times script

import os
import sys
import datetime

def path_modtime(root, filenames):
    names_mtimes = []
    for fname in filenames:
        fpath = os.path.join(root, fname)
        # avoid links
        if os.path.isfile(fpath):
            modt = os.path.getmtime(fpath)
            names_mtimes.append([fpath, datetime.datetime.fromtimestamp(modt)])
    return names_mtimes

if __name__ == '__main__':
    #topdir = '/etc'  # for original test
    if len(sys.argv) != 2:
        sys.exit("{0}: requires directory name argument".format(sys.argv[0]))
    topdir = sys.argv[1]
    if not os.path.isdir(topdir):
        sys.exit("{0}: directory {1} does not exist".format(sys.argv[0], topdir))

    # using underscore as throwaway var since the dirs is unwanted
    for root, _, fnames in os.walk(topdir):
        if fnames:
            #print "root %s" % root
            #print "fnames %s" % fnames
            for path,modtime in path_modtime(root, fnames):
                print "{} {}".format(path, modtime)
else:
    print "{}: use as main context only".format(sys.argv[0])
the "if fnames:" is there to block the cases where there's a directory that only contains other subdirectories, in that case the call to path_modtime is skipped and the outer loop goes to the next one. Note that my script is not hard coded to '/etc' but you just pass in the directory to get the mtimes from via the input argument

Last edited by marko; 27th March 2012 at 06:09 PM.
Reply With Quote
Reply

Tags
count, fedora16, file, filein, listing, modification, python, text, time

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Script that retrieves the modification time or a size of a file _cih_ Programming & Packaging 2 19th December 2006 11:46 AM
Track your file modification saad650 Security and Privacy 1 7th November 2006 08:54 PM
Automatic type text , delete file and save file problem shown in FC3 and FC2 kalpana EOL (End Of Life) Versions 1 10th June 2005 04:50 AM


Current GMT-time: 01:58 (Thursday, 20-06-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat