Data Log

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 8
Reputation: Bouzy210 is an unknown quantity at this point 
Solved Threads: 0
Bouzy210 Bouzy210 is offline Offline
Newbie Poster

Data Log

 
0
  #1
Nov 22nd, 2008
I am trying to write a script where it traverses the current folder and all subfolders and files and prints to screen the name of the files, filesize, and date last modified.

e.g.

test0.xml | 26 bytes | 11/21/08 23:35:28 (I can get it to do this)
Folder/test1.xml | 26 bytes | 11/21/08 23:35:28 (this I can't)
Folder/folder2/test2.xml | 26 bytes | 11/21/08 23:35:28

I can easily get it to work for all the folders in the current directory but always run into problems in python when I try to use all sub directories also..

  1. #!/usr/python/bin
  2. # Filename: log_file.py
  3.  
  4. """
  5. This file takes a file in a directory and writes a log file containing all the filenames, modification times and filesizes. Then on a later date, the program will check this log file to detect any changes in the directory
  6. """
  7.  
  8. import os, glob, time
  9.  
  10. cwd = os.getcwd()
  11.  
  12. log = open('log_file.txt', 'w')
  13.  
  14. for dirpath,dirs,files in os.walk(cwd):
  15.  
  16. for dirs in os.walk(cwd):
  17. for f in dirs:
  18. # psplit = os.path.split(f)[1]
  19. for file in files:
  20. size = os.stat(file)
  21. file_date = time.strftime("%m/%d/%y %H:%M:%S", time.localtime())
  22. print file,'|','%s' % size.st_size,'','bytes','|','%s' % file_date,''

Could someone explain what I need to do please.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 1
Reputation: DFooz is an unknown quantity at this point 
Solved Threads: 0
DFooz DFooz is offline Offline
Newbie Poster

Re: Data Log

 
0
  #2
Nov 22nd, 2008
Always write error messages you've got!
The problem was in os.stat(file_path).st_size. On default it get files in current work directory(i.e. where your source code is). Therefore you must write an absolute path to the file
i.e. file_path=root+'/'+file . That's why you've had the problem

PS: sorry for my bad English

  1. #!/usr/python/bin
  2. # Filename: log_file.py
  3.  
  4. """
  5. This file takes a file in a directory and writes a log file containing all the filenames, modification times and filesizes. Then on a later date, the program will check this log file to detect any changes in the directory
  6. """
  7.  
  8. import os, glob, time
  9.  
  10. cwd = os.getcwd()
  11. for root, dirs,files in os.walk(cwd):
  12. print root
  13. for file in files:
  14. file_path=root+'/'+file
  15. file_date = time.strftime("%m/%d/%y %H:%M:%S", time.localtime())
  16. print file,' | ',os.stat(file_path).st_size,'bytes | ',file_date
  17. print
Last edited by DFooz; Nov 22nd, 2008 at 9:02 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 8
Reputation: Bouzy210 is an unknown quantity at this point 
Solved Threads: 0
Bouzy210 Bouzy210 is offline Offline
Newbie Poster

Re: Data Log

 
0
  #3
Nov 22nd, 2008
ok I fixed some things..

  1. import os, glob, time
  2.  
  3. cwd = os.getcwd()
  4. datalog = open('log_file.txt', 'w')
  5.  
  6. for root,dirs,files in os.walk(cwd):
  7.  
  8. for folder in glob.glob(root):
  9. for file in glob.glob(folder + '/*.**'):
  10. file_path = os.path.split(file)[1]
  11. size = os.stat(file)
  12. file_date = time.strftime("%m/%d/%y %H:%M:%S", time.localtime())
  13. data = '%s | %s bytes | %s ' % (os.path.split(file_path)[1], size.st_size, file_date),
  14. datalog.write(data)

As far as I can tell that should now work but I get this error.

  1. Traceback (most recent call last):
  2. File "C:\Documents and Settings\Gabe\My Documents\Scripts\testing_grounds\Log_file\log_file.py", line 21, in <module>
  3. datalog.write(data)
  4. TypeError: argument 1 must be string or read-only character buffer, not tuple
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 924
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Data Log

 
0
  #4
Nov 22nd, 2008
Just take the comma off the end of line 20. Adding the comma makes it a tuple.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 924
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Data Log

 
0
  #5
Nov 22nd, 2008
I would also reccomend that when you write to the file you have a new line after each item. To do this then just go:
  1. datalog.write(data+'\n')
Hope that helps
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 8
Reputation: Bouzy210 is an unknown quantity at this point 
Solved Threads: 0
Bouzy210 Bouzy210 is offline Offline
Newbie Poster

Re: Data Log

 
0
  #6
Nov 23rd, 2008
I just have one last question. I didn't clearly explain what I was using the code for. Right now this works to write to a file the names of files in a directory with filename etc...

  1. log_file = open('log_file.txt', 'w')
  2.  
  3. for root,dirs,files in os.walk(cwd):
  4. for folder in glob.glob(root):
  5. for file in glob.glob(folder + '/*.**'):
  6. file_path = os.path.split(file)[1]
  7. size = os.stat(file)
  8. file_date = time.strftime("%m/%d/%y %H:%M:%S", time.localtime())
  9. data = '%s | %s bytes | %s ' % (os.path.split(file_path)[1], size.st_size, file_date)
  10. log_file.write(data+'\n')
  11.  
  12. log_file.close()

But I have some more code.. it checks to see if a file has already been written and compares the two logs. Why does the above code work but this doesn't? Thanks for all the help.

  1. #!/usr/python/bin
  2. # Filename: log_file.py
  3.  
  4. """
  5. This file takes a file in a directory and writes a log file containing all the filenames, modification times and filesizes. Then on a later date, the program will check this log file to detect any changes in the directory
  6. """
  7.  
  8. import os, glob, time, filecmp, tempfile
  9.  
  10. cwd = os.getcwd()
  11.  
  12. def datalog():
  13.  
  14. for root,dirs,files in os.walk(cwd):
  15. for folder in glob.glob(root):
  16. for file in glob.glob(folder + '/*.**'):
  17. file_path = os.path.split(file)[1]
  18. size = os.stat(file)
  19. file_date = time.strftime("%m/%d/%y %H:%M:%S", time.localtime())
  20. data = '%s | %s bytes | %s ' % (os.path.split(file_path)[1], size.st_size, file_date)
  21.  
  22. if os.path.exists('log_file.txt'):
  23.  
  24. # Create a 'temporary' file to store current directory's dimensions
  25. log_file2 = open('log_file2.txt', 'w')
  26. datalog()
  27. log_file2.write(data+'\n')
  28. log_file2.close()
  29.  
  30. # Compair the directory's current dimensions to previous modifications
  31. if filecmp.cmp('log_file.txt', 'log_file2.txt'):
  32. print 'No files have been modified'
  33. os.remove('log_file2.txt')
  34.  
  35. else:
  36. print 'The directory has been modified'
  37. done = raw_input("When done compariing the logs type 'done' ")
  38. if done == done:
  39. os.remove('log_file.txt')
  40. os.remove('log_file2.txt')
  41. log_file = open('log_file.txt', 'w')
  42. datalog()
  43. log_file.write(data+'\n')
  44. log_file.close()
  45.  
  46. else:
  47.  
  48. log_file = open('log_file.txt', 'w')
  49. log_file = open('log_file.txt', 'w')
  50. log_file.write(data+'\n')
  51. log_file.close()
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC