Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 25th, 2004, 10:27 AM
luckyboy luckyboy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 57 luckyboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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()

Reply With Quote
  #2  
Old June 25th, 2004, 03:33 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,585 DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
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

Reply With Quote
  #3  
Old June 27th, 2004, 07:15 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #4  
Old June 29th, 2004, 06:53 AM
luckyboy luckyboy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 57 luckyboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #5  
Old June 29th, 2004, 07:46 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #6  
Old June 29th, 2004, 09:01 AM
luckyboy luckyboy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 57 luckyboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 6 m
Reputation Power: 10
Thanks Mark
I have corrected my mistakes.

Reply With Quote
  #7  
Old July 1st, 2004, 06:24 AM
luckyboy luckyboy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 57 luckyboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #8  
Old July 1st, 2004, 09:34 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #9  
Old July 1st, 2004, 10:21 AM
luckyboy luckyboy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 57 luckyboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #10  
Old July 1st, 2004, 12:06 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #11  
Old July 1st, 2004, 12:43 PM
luckyboy luckyboy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 57 luckyboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #12  
Old July 2nd, 2004, 03:53 AM
luckyboy luckyboy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 57 luckyboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #13  
Old July 2nd, 2004, 05:39 AM
luckyboy luckyboy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 57 luckyboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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:/')

Reply With Quote
  #14  
Old July 2nd, 2004, 06:12 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #15  
Old July 2nd, 2004, 07:31 AM
luckyboy luckyboy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 57 luckyboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > file log

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap