stouputils.io.path module#
- get_root_path(relative_path: str, go_up: int = 0) str[source]#
Get the absolute path of the directory. Usually used to get the root path of the project using the __file__ variable.
- Parameters:
relative_path (str) – The path to get the absolute directory path from
go_up (int) – Number of parent directories to go up (default: 0)
- Returns:
The absolute path of the directory
- Return type:
str
Examples
> get_root_path(__file__) 'C:/Users/Alexandre-PC/AppData/Local/Programs/Python/Python310/lib/site-packages/stouputils' > get_root_path(__file__, 3) 'C:/Users/Alexandre-PC/AppData/Local/Programs/Python/Python310'
- relative_path(file_path: str, relative_to: str = '') str[source]#
Get the relative path of a file relative to a given directory.
- Parameters:
file_path (str) – The path to get the relative path from
relative_to (str) – The path to get the relative path to (default: current working directory -> os.getcwd())
- Returns:
The relative path of the file
- Return type:
str
Examples
>>> relative_path("D:/some/random/path/stouputils/io.py", "D:\\some") 'random/path/stouputils/io.py' >>> relative_path("D:/some/random/path/stouputils/io.py", "D:\\some\\") 'random/path/stouputils/io.py'
- super_copy(
- src: str,
- dst: str,
- create_dir: bool = True,
- symlink: bool = False,
Copy a file (or a folder) from the source to the destination
- Parameters:
src (str) – The source path
dst (str) – The destination path
create_dir (bool) – Whether to create the directory if it doesn’t exist (default: True)
symlink (bool) – Whether to create a symlink instead of copying (Linux only)
- Returns:
The destination path
- Return type:
str
- super_open(
- file_path: str,
- mode: str,
- encoding: str = 'utf-8',
Open a file with the given mode, creating the directory if it doesn’t exist (only if writing)
- Parameters:
file_path (str) – The path to the file
mode (str) – The mode to open the file with, ex: “w”, “r”, “a”, “wb”, “rb”, “ab”
encoding (str) – The encoding to use when opening the file (default: “utf-8”)
- Returns:
The file object, ready to be used
- Return type:
open
- read_file(file_path: str, encoding: str = 'utf-8') str[source]#
Read the content of a file and return it as a string
- Parameters:
file_path (str) – The path to the file
encoding (str) – The encoding to use when opening the file (default: “utf-8”)
- Returns:
The content of the file
- Return type:
str
- replace_tilde(path: str) str[source]#
Replace the “~” by the user’s home directory
- Parameters:
path (str) – The path to replace the “~” by the user’s home directory
- Returns:
The path with the “~” replaced by the user’s home directory
- Return type:
str
Examples
> replace_tilde("~/Documents/test.txt") '/home/user/Documents/test.txt'
- clean_path(
- file_path: str,
- trailing_slash: bool = True,
Clean the path by replacing backslashes with forward slashes and simplifying the path
- Parameters:
file_path (str) – The path to clean
trailing_slash (bool) – Whether to keep the trailing slash, ex: “test/” -> “test/”
- Returns:
The cleaned path
- Return type:
str
Examples
>>> clean_path("C:\\Users\\Stoupy\\Documents\\test.txt") 'C:/Users/Stoupy/Documents/test.txt'
>>> clean_path("Some Folder////") 'Some Folder/'
>>> clean_path("test/uwu/1/../../") 'test/'
>>> clean_path("some/./folder/../") 'some/'
>>> clean_path("folder1/folder2/../../folder3") 'folder3'
>>> clean_path("./test/./folder/") 'test/folder/'
>>> clean_path("C:/folder1\\folder2") 'C:/folder1/folder2'