Discussion:
i have problem with glob.glob() in remotely directory
(too old to reply)
lameck kassana
2009-02-26 09:05:06 UTC
Permalink
hey i want to count number of files in remote computer

example of my code is

import glob
import os
import time
from datetime import date
today=date.today()
dir_count, file_count=0, 0

for files in glob.glob('\\192.168.0.45\files\*.txt'):
file_count += len(files)
print '----the count of ',today, '-------'
print 'Found', dir_count, 'sub-directories in cwd'
print 'Found', file_count, 'files in cwd'
print 'Found', dir_count + file_count, 'files & sub-directories in cwd'
filename="reconciliation.txt"
file_string= str(file_count)+','+ str(today)+'\n'
File=open(filename,"a")
File.writelines( file_string)
File.close()



i get zero results since glob.glob( )can not traverse in \\192.168.0.45

can I get help please how can i do that???
Chris Rebert
2009-02-26 09:16:16 UTC
Permalink
Post by lameck kassana
hey i want to count number of files in remote computer
example of my code is
import glob
import os
import time
from datetime import date
today=date.today()
dir_count, file_count=0, 0
Remember that *backslash is the escape character* in Python, so you
need to double-up your backslashes in the string literal (or just use
forward slashes instead, Windows doesn't seem to care for Python in
most cases). Right now, the path really only starts with 1 backslash
and it has a formfeed character in it (\f), so it's obviously invalid;
thus, your problem.

So you want:
#looks ugly, doesn't it?
for files in glob.glob('\\\\192.168.0.45\\files\\*.txt'):

Or:
#will probably but not assuredly work
for files in glob.glob('//192.168.0.45/files/*.txt'):

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
lameck kassana
2009-02-26 11:28:35 UTC
Permalink
i did try but still not working.But also i try os.walk() for remote
computer like os.walk('\\192.168.0.45') it also failed>
Thats it is my main problem do i need any new imports besides import os
Post by Chris Rebert
Post by lameck kassana
hey i want to count number of files in remote computer
example of my code is
import glob
import os
import time
from datetime import date
today=date.today()
dir_count, file_count=0, 0
Remember that *backslash is the escape character* in Python, so you
need to double-up your backslashes in the string literal (or just use
forward slashes instead, Windows doesn't seem to care for Python in
most cases). Right now, the path really only starts with 1 backslash
and it has a formfeed character in it (\f), so it's obviously invalid;
thus, your problem.
#looks ugly, doesn't it?
#will probably but not assuredly work
Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
Tim Roberts
2009-03-01 05:24:29 UTC
Permalink
Post by lameck kassana
i did try but still not working.But also i try os.walk() for remote
computer like os.walk('\\192.168.0.45') it also failed>
Of course it did, for two different reasons. First, you can't just walk an
IP address. You have to specify one of the shares that the machine
exposes. Second, you STILL have a backslash problem.

If that machine has a network share called "files", you could say
os.walk( '\\\\192.168.0.45\\files' )
or
os.walk( r'\\192.168.0.45\files' )
Post by lameck kassana
Thats it is my main problem do i need any new imports besides import os
No.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Steve Holden
2009-02-26 12:56:19 UTC
Permalink
Post by Chris Rebert
Post by lameck kassana
hey i want to count number of files in remote computer
example of my code is
import glob
import os
import time
from datetime import date
today=date.today()
dir_count, file_count=0, 0
Remember that *backslash is the escape character* in Python, so you
need to double-up your backslashes in the string literal (or just use
forward slashes instead, Windows doesn't seem to care for Python in
most cases). Right now, the path really only starts with 1 backslash
and it has a formfeed character in it (\f), so it's obviously invalid;
thus, your problem.
#looks ugly, doesn't it?
#will probably but not assuredly work
Or:

for files in glob.glob(r'\\192.168.0.45\files\*.txt'):

Raw string literals are very useful for handling strings with lots of
backslashes in them.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Continue reading on narkive:
Loading...