The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
file log
Discuss file log in the Python Programming forum on Dev Shed. file log Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 25th, 2004, 10:27 AM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 57
Time spent in forums: 1 h 6 m
Reputation Power: 10
|
|
|
Log File
Hi all
I have 6 servers and each of them has a log file called all.log .
I have a text file contains all the unc of the server.Now
I am trying to write a code which will look at the text file and call one unc path a time and display the total line of the all.log file for each server seperatley. Here is the code i wrote may be it's wrong can someone help
Thanks
Code:
import os
os.chdir('/')
os.chdir('c:/')
count=0
serverpath=file('serverfile.txt','r')
serverpath1=serverpath.readlines()
for line in serverpath1:
line=line.strip()
serverunc=("\\\\%s\\mat\\rulip\\all.log")%(line)
openconsole=file("all.log",).readlines()
print len(openconsole)
serverpath.close()
|

June 25th, 2004, 03:33 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
As far as I can tell from a quick read through, your code should work OK. However it could be improved in terms of efficiency and programming style. Here are my observations:
* you do os.chdir twice, for no apparent reason. If this is to handle different operating systems then test the OS type and do it conditionally
* you set count = 0 but then never use it. I presume this is left over from old code that got deleted.
* If you are using a recent version of Python then you do not need to read the serverpath file into a list to iterate over it. You can iterate over the file directly (see my revised code, below)
* similarly you read the entire log files into memory just to count the lines. Log files tend to grow huge so this could cause memory problems as well as being inefficient. I would iterate over the file and count the lines explicitly
* the string "\\\\%s\\mat\\rulip\\all.log" is better expressed using a raw string, so you do not have to escape all the backslashes: r"\\%s\mat\rulip\all.log"
Here is my revised version:
Code:
import os
os.chdir('/')
for line in file('serverfile.txt','r'):
line=line.strip()
serverunc=r"\\%s\mat\rulip\all.log" % line
count = 0
for logline in file("all.log"): count += 1
print count
Dave - The Developers' Coach
|

June 27th, 2004, 07:15 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Quote: | Originally Posted by DevCoach * the string "\\\\%s\\mat\\rulip\\all.log" is better expressed using a raw string, so you do not have to escape all the backslashes: r"\\%s\mat\rulip\all.log" |
Worth noting that just because Windows uses backslashes you dont have to, or i've never had cause to since using forwardslashes works fine with Python. This may be a problem in Perl that has been carried over to Python, but im not 100% on that one
Mark.
__________________
programming language development: www.netytan.com – Hula
|

June 29th, 2004, 06:53 AM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 57
Time spent in forums: 1 h 6 m
Reputation Power: 10
|
|
|
log file
Thanks to all the reply
I am a window user
I have modified the code to my need as follow and it works fine. Now i want to copy the total of each log file to my c:\drive the reason beeing is in future i want to compare the file from the server and from the c:\ and display the differences and replace the new file to the one in c:\ drive
Code:
import os
os.chdir('/')
os.chdir('c:/')
for line in file('serverfile.txt','r'):
line=line.strip()
os.chdir(r"\\%s\mat\rulip" % line)
openlog=file("all.log").readlines()
total=len(openlog)
print total
The text file from c:\ contains 6 unc path
(1) unc 1, (2)unc2, (3)unc3 (4)unc4, (5)unc5, (6)unc,
The all.log file from server should copied to c:\drive as unc1.lo, unc2.log, unc3.log...... unc6.log
Any help
Thanks in advance
|

June 29th, 2004, 07:46 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
You're still changing directories twice right at the beginning of your program - which as dave said, may have something to do with cross platform compatability... but since cding to C:/ on *nix should cause an error and cding to / will do nothing. Also, this seems expecially pointless since your then open a file and cd to the values within this.
Also, you should choose to use either backslashes or forwardslashes, mixing both isnt good style and could end up being a little comfusing.
Add these few refinments and you end up with somthing like this.
Code:
import os
path = 'C:/serverfile.txt'
for line in file(path):
os.chdir('//%s/mat/rulip' % line.strip())
openlog = file('all.log').readlines()
total = len(openlog)
print total
As for comparing the files you should probably look at the difflib module in the standard library or maybe even the filecmp module.
http://www.python.org/doc/2.3.4/lib/module-difflib.html
http://www.python.org/doc/2.3.4/lib/module-filecmp.html
Hope this helps,
Mark.
Last edited by netytan : June 29th, 2004 at 07:50 AM.
|

June 29th, 2004, 09:01 AM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 57
Time spent in forums: 1 h 6 m
Reputation Power: 10
|
|
|
Thanks Mark
I have corrected my mistakes.
|

July 1st, 2004, 06:24 AM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 57
Time spent in forums: 1 h 6 m
Reputation Power: 10
|
|
|
I am still not sure how to copy a file from the server to my local disk and compare the files from the local disk to the one in server for example read the all log file from each of my 6 servers and copy it to my local disk naming it as unc1.log unc2.log and on...
Can anyone help me to understand some examples would be nice then pointing to some web sites.
|

July 1st, 2004, 09:34 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
You can use the urllib module as illustrated below to retrive a file from a remote location and save it to you local disk..
Code:
>>> import urllib
>>> page = urllib.urlopen('http://www.addess.com/to/file')
>>> file('unc2.log', 'w').write(page.read())
>>>
Note: This is just from my head as i dont have access to IDLE right for now.
http://www.python.org/doc/2.3.4/lib/module-urllib.html
After that you need to compare the file to the one already on your local machine, but im not sure what kind of thing you are looking for here so i cant give you an example for this bit  .
Take care,
Mark.
|

July 1st, 2004, 10:21 AM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 57
Time spent in forums: 1 h 6 m
Reputation Power: 10
|
|
Code:
import os
path='c:/serverfile.txt'
for line in file(path):
os.chdir('//%s/mat/rulip' % line.strip())
openlog = file('all.log').readlines()
total=len(openlog)
#copyfile('total','c://')
print '%6s %20s' % (total ,line)#,line)
The above code displays something like
unc1
4570
unc2
256
unc3
124
......
Look at #copyfile('total','c://')
All I want to do now is put each server's total lines of all.log beside the server name like .
(serverfile.txt is in c:/drive)
Serverfile.txt at begining will look like
unc1
unc2
unc3
and on
After collecting the total number of lines from the all.log from each server.
unc1 = (total number of all.log line) i.e 4570
unc2 = 256
unc3 = 124
and on
Thanks in advance
|

July 1st, 2004, 12:06 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Are you sure that you dont get the number displayed before the name? Because from looking at your code thats what i would expect. Anyway if you change the last line to something like...
Code:
print '%s = %s' % (line.strip(), total)
Then you should get the output you wanted. If this is a localserver then you can use the shutil module to make the copy for you:
http://www.python.org/doc/2.3.4/lib/module-shutil.html
Let me know how it goes,
Mark.
|

July 1st, 2004, 12:43 PM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 57
Time spent in forums: 1 h 6 m
Reputation Power: 10
|
|
|
Yes you are right mark according to my code it displays the number first then the name of the server. It is a local server.
Thanks
|

July 2nd, 2004, 03:53 AM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 57
Time spent in forums: 1 h 6 m
Reputation Power: 10
|
|
Why am i getting the following error
Traceback (most recent call last):
File "c:\Pyth2\servers.py", line 8, in -toplevel-
print '%2s %s' % (line.strip(),total.rjust(10))
AttributeError: 'int' object has no attribute 'rjust'
the code is
Code:
import os
path='c:/serverfile.txt'
for line in file(path):
os.chdir('//%s/mat/rulip' % line.strip())
openlog = file('all.log').readlines()
total=len(openlog)
print '%6s %20s' % (line.strip,total.rjust(10))
Thanks
|

July 2nd, 2004, 05:39 AM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 57
Time spent in forums: 1 h 6 m
Reputation Power: 10
|
|
Unable to copy don't know why???????????
Code:
import os
path='c:/serverfile.txt'
list=[]
eq= '='
for line in file(path):
os.chdir('//%s/mat/rulip' % line.strip())
openlog = file('all.log').readlines()
total=len(openlog)
list=[line.strip(),eq,total]
for serverfile3 in list:
shutil.copy('serverfile3', 'c:/')
|

July 2nd, 2004, 06:12 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
The reason you cant call the rjust() method on 'total' is because line is an int type, not a string; so you cant use string a method. You can apply the justification using string formatting like to what you are already doing with the 'line' variable.
Code:
print '%2s = %10s' % (line.strip, total)
Or you can convert total to a string and then call rjust() that way...
Code:
print '%2s = %s' % (line.strip, str(total).rjust(10))
As for not being able to copy the file, it seems to me that your program doesnt import the shutil module. Right?
Also you call shutil.copy() inside a loop but with the same parameters. Why are you trying to copy the same file three times?
Hope this helps,
Mark.
|

July 2nd, 2004, 07:31 AM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 57
Time spent in forums: 1 h 6 m
Reputation Power: 10
|
|
|
Hi mark
I want to keep the output on a seperate file in my local disk.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
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
|
|
|
|
|