Parse directory path in Python string

I'm attempting to parse a directory in Python.  Below is the code I'm using.

for root, dirs, files in os.walk("Z:\MY_DIRECTORY", topdown=False):
    for name in files:
        fname = str(os.path.join(root, name))
        splitdir = fname.split("\")

Open in new window


I get an error that says, "invalid syntax" error.

I eventually want to write each folder/directory to it's own field in a database.  So, I would have a column with the entire path and then several columns that have the individual directory names that make up the path.  Am I on the right track, or is there an easier way to do this?
bobjones1208Asked:
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.

Walter RitzelSenior Software EngineerCommented:
Here is how the code worked for me:
import sys
import os
for root, dirs, files in os.walk("Z:\\MY_DIRECTORY", topdown=False):
	for name in files:
		fname = str(os.path.join(root, name))
		splitdir = fname.split("\\")

Open in new window

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
gelonidaCommented:
Perhaps I don't understand, but this sounds strange to me:

do you want to say, that for following path
z:\top1\sub1\sub2\sub3\sub4\test.txt

Open in new window


you'd like to create ONE row in the data base?
that one column would contain, the entire string,
one column contain:
z:\
the next one
top1
the next one
sub1
. . .
the last one
'test.txt'

What seems a little strange is, that you had to create as many columns as you could have levels of directories.

Do you have a little more context?
Is the database often written / modified? how many accesses do you assume

is the data base often read.
What would be the kind of questions, that you'd like to ask the DB with a query.

How bug would the DB be.
All this might help to understand whether your approach is good.

Do you want to access the database directory or with an ORM (django or others?)

Another small comment:
In order to make your code more cross platform you should
use
os.path.sep

Open in new window

instead of
'\\'

os.path.sep will be '/' for linux and '\\' for windows
0
bobjones1208Author Commented:
That did the trick.  Thanks.
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.