File in folder on Windows network share using python

For this question lets say the network share is \\192.168.1.1\d$\testfolder\
preferred method: Would like to store all the file names in a python list\
other option: Store all the file names to an output.txt file in c:\resultsfolder\

I am using python 3.4 due to getting the script to integrate with tkinter.
LVL 1
m_travisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gelonidaCommented:
1.) From python's point of view there shouldn't be anything special about a network share.  It should be accessible like a local directory.

Just do the correct escaping for the path name. (note the  r before the double quotes.)

source_dir = r"C:\mydirectory"  # path to a local directory
source_dir = r"\\192.168.1.1\d$\testfolder" # path to a remote directory.

So  to be sure, that you don't have any permission issues:

Does following code work?
import os

source_dir =  r"\\192.168.1.1\d$\testfolder" 
for entry in os.listdir(source_dir):
    print("found %r" % entry)

Open in new window



If you want to have ALL files recursively, then you can use os.walk()
I can give you an example if you like to.
The current example lists all files and directory in the remote network share.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
m_travisAuthor Commented:
Thank you, worked perfectly.

What is the r for?
0
gelonidaCommented:
if you don't use the r you had to escape every backslash which is quite tedious

example:
r"\\192.168.1.1\d$\testfolder"

Open in new window

is equivalent to
"\\\\192.168.1.1\\d$\\testfolder"

Open in new window

which is less readable and more error prone


There is only one exection (in which I fell immediately, when I heard first about the r prefix.

If the last character of a string is a \ , then you have still to escape it.

so
r"c:\"

Open in new window

is invalid
you had to type
r"C:\\"

Open in new window

0
m_travisAuthor Commented:
Thank you for the explanation. I also posted the following python question that you might be able to help.

Other python question
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.