0

I would like to split "C:\My Work\Tester Related\A_B_C.txt" into [C:\, My Work, Tester Related, A, B, C, txt] in Python.

I just started learning Python. I did import os.path module, and played with some of the functions in the interactive prompt but was not able to get the desired result. Thanks for the help. I was able to use os.path.basename to get the file, but was not able to split the file into components based on the given delimiter '_'.

5
  • 1
    Sounds like a job for split(file_name,"_"). But you want it split on several delimiters. Hm. Commented Aug 4, 2014 at 20:49
  • 1
    Consider replacing _ with the os.pathsep first. Then you'll have a normalized path of "C:\My Work\Tester Related\A\B\C.txt" Commented Aug 4, 2014 at 20:51
  • 1
    It's okay to first separate file path and file name, and split each into components based on their respective delimiters. It does not have to be one step to do all. :) Commented Aug 4, 2014 at 20:51
  • What version are you using? If you're on 3.x you could use * unpacking with split Commented Aug 4, 2014 at 21:04
  • Sorry, didn't mention, but I am using version 3.4.1 on Win 7 pro Commented Aug 4, 2014 at 21:13

3 Answers 3

2
s = "C:\\My Work\\Tester Related\\A_B_C.txt"

import re
print (re.split(r"\\|\.|\_",s))
['C:', 'My Work', 'Tester Related', 'A', 'B', 'C', 'txt']
Sign up to request clarification or add additional context in comments.

7 Comments

I got syntax error:>>> s = "C:\My Work\Tester Related\A_B_C.txt" >>> print re.split(r"\\|\.|_",s) SyntaxError: invalid syntax >>>
I suggested some edits to Padraic's answer that should resolve the syntax issue.
@Murchak you might need r"C:\My Work\Tester Related\A_B_C.txt" on windows
@Murchak, you also need parens for printing
Thanks Kevin! Of course the parentheses have to be there. But what I was missing was the import re statement. That did the trick. Thanks all. I did search this before posting this question, but couldn't find an answer. So I posted this specific example to not only help me, but others who may have similar questions. The different methods posted here are very instructive. Thanks all.
|
0
original_path = "C:\My Work\Tester Related\A_B_C.txt"

# Must quote the backslash in this case.
split_path = original_path.split("\\")

# At this point, split_path looks like:
# ['C:', 'My Work', 'Tester Related', 'A_B_C.txt']

split_filename = split_path[-1].split("_")
# split_filename is ['A', 'B', 'C.txt']

split_ext = split_filename[:-1].split(".")
# split_ext is ['C', 'txt']

split_filename = split_filename[:-1] + split_ext
# split_filename is ['A', 'B', 'C', 'txt']

split_path = split_path[:-1] + split_filename
# ['C:', 'My Work', 'Tester Related', 'A', 'B', 'C', 'txt']

It's a little hacky but it'll work.

2 Comments

Thanks Kevin. But can the split function take in the whole file path as an argument with a given delimiter to do the splitting? Or must I define a new variable/object and assign to it the file path, then use the split method on this object where the argument of the split method is only the delimiter?
Hi Murchak, the split function can only split one element at a time. You could, however, use Padraic's excellent answer to do what you'd like in a much more concise way.
0

an ugly two liner!

import itertools
txt = r"C:\\My Work\\Tester Related\\A_B_C.txt"

result = list(itertools.chain(*map(lambda x: x.split('_'), txt.split(os.path.sep))))
result = result[:-1] + list(os.path.splitext(result[-1]))

A better method is to use re, as @PaedricCunningham did in his answer.

re.split(r"\\|\.|_", txt)

4 Comments

Thanks Adam. Interesting! I briefly skimmed through defining functions using the lambda way. I'll try to understand your code to see how it works.
@Murchak it's an interesting academic lesson, but don't write code this way. It's unreadble.
I tried, but got invalid syntax error. I did import itertools first, then type exactly as suggested, got syntax error
@Murchak try it again. I realized I made several errors and edited about the time you made that last comment :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.