2
  • I am trying to find all .SQL files from my folder and it's sub folders.
  • I know how to get them, but I get output like
c/folder1/folder2/file.sql
  • I want output like
folder1/folder2/file.sql
Trenton McKinney
63.2k41 gold badges170 silver badges214 bronze badges
asked Jul 15, 2020 at 19:07
1
  • 1
    Share your code so others can replicate and modify for you Commented Jul 15, 2020 at 19:13

2 Answers 2

1

You can use the str.split():

s = 'c/folder1/folder2/file.sql'
s = '/'.join(s.split('/')[1:])
print(s)

Output:

folder1/folder2/file.sql

UPDATE: More dynamic:

s = 'c/few folders/folder 1/folder 2/file.sql and I get output folder 1/folder 2/file.sql'
s = 'folder 1' + s.split('folder 1')[1]
answered Jul 15, 2020 at 19:13
Sign up to request clarification or add additional context in comments.

2 Comments

Many Thanks Ann, I was looking for something similar.. is it possible to make this more dynamic.. example- c/few folders/folder 1/folder 2/file.sql and I get output folder 1/folder 2/file.sql
@shubhamgarg Updated.
0

You might want to use pathlib from the standard library, in particular pathlib.Path and the glob method, like this:

import pathlib
parent = pathlib.Path(path_to_parent_directory)
sql_paths = sorted(parent.glob("**/*.SQL"))

To get the relative paths and the strings:

relative_paths = [p.relative_to(parent) for p in sql_paths]

You can keep working with these path objects, or you can convert them to strings with the as_posix method: [p.as_posix() for p in relative_paths].

answered Jul 15, 2020 at 19:30

1 Comment

Thanks mate .. I will try this

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.