I am using pathlib instead of using the functions defined inside the os library. I consider it simpler and cleaner to use. Pathlib takes an object oriented approach to filepaths. See https://realpython.com/python-pathlib/
Using pathlib, if you have a {python}base_fp
, how would you create a {python}ressource_fp
(just the filepath, not creating a file or folder), that is simply the path to the folder {python}"ressources"
at {python}base_fp
?
?
from pathlib import Path
base_fp = Path("...")
ressource_fp = base_fp / "ressources"
# another option
base_fp.joinpath("ressources")
Using pathlib, please get the current working directory (the directory, where your python script is located).
?
from patlib import Path
print(Path.cwd())
Using pathlib, please iterate through all files and folders at {python}filepath
. Call {python}handle_files(file)
if it is a file and {python}handle_folder(folder)
if it is a folder.
?
from pathlib import Path
filepath = Path('/path/to/your/folder')
# This loop will itearte through
for item in filepath.iterdir():
if item.is_file():
handle_file(item)
if item.is_dir():
handle_folder(item)
Using pathlib, please {python}print("file with pattern found")
for each file in folder {python}folder_fp
, that fits regex pattern {python}r_pattern
. Please ignore lower/higher case.
?
from pathlib import Path
import re
for item in folder_fp.iterdir():
if item.is_file():
filename = item.stem
match = re.search(pattern=pattern, string=filename, flags=re.IGNORECASE)
if match:
print("file with pattern found")