Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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 May 19th, 2004, 06:03 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: 46 m 58 sec
Reputation Power: 5
Read file from server

[QUOTE]

Hi
I have written a code to connect to a sever,path ,open a file , count the lines and display the the contents of the file and the number of lines,

Now I am confused how to copy the file to my hard disk, Can someone help me
Thanks


Code:
import os,sys
#os.chdir('/')
os.chdir('\\\\server name and path')
openfile=open('file name','rb',)
count=0
while 1:
    readfile=openconsole.read()
    if not readfile:
        break
    count=+readfile.count('\n') 
    print readfile
    print count
openfile.close()

Reply With Quote
  #2  
Old May 19th, 2004, 07:43 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,536 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 3 m 4 sec
Reputation Power: 63
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
If the file is accessable though the internet then your best bet is probably the urllib module; if only because its so easy to use. Heres an example of how this might work:

Code:
>>> import urllib
>>> page = urllib.urlopen('http://www.python.org/').read()
>>> print page
...


Hope this helps,

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old May 19th, 2004, 04:18 PM
sfb sfb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 447 sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 1 h 43 m 45 sec
Reputation Power: 9
It looks like you're getting it from a UNC path, like "\\server1\files\somefile.ext", so take a look at the shutil module ('Shell Utilities')

Code:
>>> import shutil
>>> shutil.copy('\\\\server\\files\\file.ext', 'c:\\')

Reply With Quote
  #4  
Old May 19th, 2004, 09:53 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,536 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 3 m 4 sec
Reputation Power: 63
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
Shutil can only be used to move a file on your local machine, so not much help here. Heres a rewriten version of the line count program if it is of any help to anyone.

Code:
count = 0

for line in file('path/to/local/file.dat', 'rb'):
    count = count + 1
    print '%s: %s' % (count, line)
print 'Total lines:', count


This should print something like this:

Quote:
1: line one
2: line two
3: line three
Total lines: 3


Mark.

Reply With Quote
  #5  
Old May 20th, 2004, 02:02 AM
sfb sfb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 447 sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 1 h 43 m 45 sec
Reputation Power: 9
Quote:
Originally Posted by luckyboy
Now I am confused how to copy the file to my hard disk


Quote:
Originally Posted by netytan
Shutil can only be used to move a file on your local machine, so not much help here.


It can copy too...

Reply With Quote
  #6  
Old May 20th, 2004, 07:06 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,536 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 3 m 4 sec
Reputation Power: 63
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
Oops, i missed that line in luckies post . Anyway just to clarify

You can use use shutil to move and or copy files around, but only on the local machine. So retriving a file from a webserver is a little outa its reach. In which case you'll probably want to use the urllib module's.

In this example he was cd'ing to the directory which you dont need to do since file() accepts a full path to a file. Which is where the comfusion about your post came in.

Mark.

Reply With Quote
  #7  
Old May 20th, 2004, 04:26 PM
sfb sfb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 447 sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 1 h 43 m 45 sec
Reputation Power: 9
Quote:
Originally Posted by netytan
You can use use shutil to move and or copy files around, but only on the local machine.

It works over UNC shares too - \\server\file, which the original poster seems to be using:

"os.chdir('\\\\server name and path')"

Quote:
So retriving a file from a webserver is a little outa its reach. In which case you'll probably want to use the urllib module's.

Agreed, but ... what webserver?

Quote:
In this example he was cd'ing to the directory which you dont need to do since file() accepts a full path to a file. Which is where the comfusion about your post came in.
Mark.

Yes, ok, I wasn't really looking at the line-count part. I guess the ideal answer depends on whether luckyboy wants to:

Read the file on the server to manually make a copy of it locally, counting the lines in the process, or...

Count the lines in the file. Also, somehow make a copy locally.

---

Code:
data = file("\\\\server\\path\\file.txt").readlines()

print 'Total lines:', len(data)



(file defaults to opening with 'r'. It doesn't seem to make much sense to count the lines in a binary file...)

Reply With Quote
  #8  
Old May 21st, 2004, 12:13 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,536 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 3 m 4 sec
Reputation Power: 63
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
Ah, when somone says server i alutomatically assume that we're talking remote server, but surly if the server is local (a localhost - which lucky didn't mention) to the machine/network then all you have to do is provide the path to the file on the server to file().

But yes, this being the case, you could use shutil to make a copy of the file in your working directory if you so wished; although slightly pointless since you can use the file its current position on the system.

I'm not sure what your talking about with the while 'r' rather than 'rb' flag thing since in the example he's using 'rb'.

Edit: your example does count the total number of lines but still can't mimic the output of either examples (provided by myself or lucky) so as a way of retiriving a total number it works, but not quite what lucky wanted

Mark.

Last edited by netytan : May 21st, 2004 at 12:17 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Read file from server


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT