How can I split string to several folders? [duplicate] - python

This question already has answers here:
How to split a dos path into its components in Python
(23 answers)
Closed 4 years ago.
I have a case like this:
Path = 'C:\Intel\ExtremeGraphics\CUI\Resource'
I want to split this string to list with the string folders names:
['C:', 'Intel', 'ExtremeGraphics', 'CUI, 'Resource']
How can I do that in the shortest way?

folders = Path.split('\')
It will return you the required list of the folders

Related

How to extract specific path from string using Python? [duplicate]

This question already has answers here:
s3 urls - get bucket name and path
(11 answers)
Closed 2 years ago.
i have string name Path:
Path = 's3://ihsm-dl-automotive-dev/auto_target_marketing_data/Testing-sandbox/PP_PROCESS_TESTING_DEEPAK/Buick_Encore_studyfile'
i want to create two string and store in two different variables as given below;
bucket = 'ihsm-dl-automotive-dev'
path_new = 'auto_target_marketing_data/Testing-sandbox/PP_PROCESS_TESTING_DEEPAK/Buick_Encore_studyfile'
i have tried few option like split by delimiter like Path.split('/') but it gives me in list form.
can anyone help me with this?
If you already have a constant bucket name, why not split with that name and use that to split and then slice the string with :
Path.split(bucket)[1][1:]
OR
If you dont have a constant bucket string, use:
a=Path.split("/")
bucket,path=a[2],"/".join(a[3:])
This will give you a list, but you can join them using join

python sort file path based on created time [duplicate]

This question already has answers here:
How do I get file creation and modification date/times?
(12 answers)
How do you get a directory listing sorted by creation date in python?
(19 answers)
Closed 2 years ago.
I have python array with filepath in it, I need to sort the files path based on created date. Is there any inbuilt functionality exist for it.
files = ["/path1/filename.text", "/path2/filename.text", "/path2/filename.text"]

Make a list with multiple possible strings from file names with regex [duplicate]

This question already has answers here:
Regex match one of two words
(2 answers)
Closed 3 years ago.
I want to make a list of several PNG in a folder based on multiple references. So in the list I want the PNG that have the string "7029113" OR "7031503" in their name. This is what I got so far, I only need to know how to do OR with regex, and probably my wildcards are wrong too I'm not sure.
render_path = "C:/BatchRender/Renaming"
os.chdir(render_path)
list_files = glob.glob("*.png")
r = re.compile(".*7029113.*" OR ".*7031503.*")
list_40 = list(filter(r.match, list_files))
This is one way of doing it.
r = re.compile('.*(7029113|7031503).*')

Manipulate variables in Python [duplicate]

This question already has answers here:
How to get only the last part of a path in Python?
(10 answers)
Closed 5 years ago.
Is it possible to manipulate a variable, for example:
file = "/Python/work.txt"
to just list work.txt, without /Python? excluding everything on the left of the "/"?
Thank you
Of course! Simply do this:
file = "/Python/work.txt"
excluded = file.split("/")[-1]
This would return "work.txt" in the excluded variable.

Python: split on two different expressions? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Python strings split with multiple separators
Is there a way in Python to split a string on two different keys?
Say I have a string like this:
mystr = "wketjwlektjwltjkw<br/>wwwweltjwetlkwjww" + \
"wwetlwjtwlet<strong>wwwwketjwlektjwlk</strong"
I'd like to split on EITHER <br/>wwww or <strong>wwww.
How should I do this?
Thanks!
import re
re.split(r'<(br\/|strong)>wwww', mystr)[::2]

Categories