Skip to content

Instantly share code, notes, and snippets.

@nobucshirai
Last active June 3, 2024 00:28
Show Gist options
  • Save nobucshirai/3369addd57be0e1546cd34aafffe0c07 to your computer and use it in GitHub Desktop.
Save nobucshirai/3369addd57be0e1546cd34aafffe0c07 to your computer and use it in GitHub Desktop.
`toggle_dir.py` is a simple Python script that toggles between two specific directory structures: `src/` and `tests/`. It checks the current working directory and, if it is within a `src/` directory, it switches to the corresponding `tests/` directory and vice versa. The typical command: cd `toggle_dir.py`
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
def switch_directory():
current_path = os.getcwd()
base_dir = os.path.abspath(current_path)
if "src" in base_dir.split(os.sep):
# Switch from src/ to tests/
test_dir = base_dir.replace("src", "tests")
return test_dir
elif "tests" in base_dir.split(os.sep):
# Switch from tests/ to src/
src_dir = base_dir.replace("tests", "src")
return src_dir
else:
print("Current directory is not within 'src/' or 'tests/' paths.")
return None
def main():
new_path = switch_directory()
if new_path:
print(new_path)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment