#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    57
    Rep Power
    11

    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()
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    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:\\')
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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:

    1: line one
    2: line two
    3: line three
    Total lines: 3
    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    Originally Posted by luckyboy
    Now I am confused how to copy the file to my hard disk
    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...
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    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')"

    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?

    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...)
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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 01:17 AM.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo