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

25th March 2012, 01:56 AM
|
|
Registered User
|
|
Join Date: Feb 2012
Location: canada
Posts: 12

|
|
|
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?
|

26th March 2012, 10:17 AM
|
|
Registered User
|
|
Join Date: Jul 2009
Location: England, UK
Posts: 823

|
|
|
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).
|

26th March 2012, 10:49 AM
|
|
Registered User
|
|
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,150

|
|
|
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...
|

26th March 2012, 11:36 PM
|
|
Registered User
|
|
Join Date: Feb 2012
Location: canada
Posts: 12

|
|
|
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)))
|

27th March 2012, 04:10 AM
|
 |
Registered User
|
|
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 5,490

|
|
|
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.
|

27th March 2012, 06:35 AM
|
|
Registered User
|
|
Join Date: Feb 2012
Location: canada
Posts: 12

|
|
|
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
|

27th March 2012, 03:43 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,150

|
|
|
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)
|

27th March 2012, 04:47 PM
|
 |
Registered User
|
|
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 5,490

|
|
|
Re: listing file, modification time, and count to text file---in python via fedora16
Quote:
Originally Posted by BrandonAdam
~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:
(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:
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.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Current GMT-time: 01:58 (Thursday, 20-06-2013)
|
|
 |
 |
 |
 |
|
|