Stringpath for treating a string as a path
stringpath is a python script for simulating the 'cd' command on a string that contains a path.
Please report errors directly to my email: [email protected]
Download with git
:
https://github.com/PascalVallaster/stringpath.git
or download the ZIP
file.
- Python 3.4^
- Standard Library
from stringpath import change_directory
cwd = r"C:\my/current\path"
cd = r"..\i/want\to\cd/in/here"
new_cwd = change_directory(cwd, cd)
For more usage details see the docs in the functions itself
Imagine your program has to execute sys-commands in different (user-depending) directories with subprocess which will be determined by the user using the command 'cd'. The problem is, you don't want to change the current working directory in order to avoid problems and security threats. This tool allows you to accomplice that.
Example:
import subprocess
import stringpath
command = "dir"
cwd = "C:/MyFiles/MyAppFolder/"
print("'cd' into a different location")
cd = input(cwd + ">")
cwd = stringpath.change_directory(cwd, cd)
subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=cwd)