I coincidentally found out that I cannot change the actual directory from within a python-code. My Test-Program is as follows:
from os import system
def sh(script):
system("bash -c '%s'" % script)
sh("cd /home")
sh("pwd")
The output of pwd
is not /home
, but the directory where the code above lives.
Can someone explain why this happens?
You can change the directory by just typing "cd DirectoryPath" into the command prompt. Replace "DirectoryPath" with either a full path or the name of a folder in the current folder to go into that folder. You can type "cd .." to "up" or "out of" the current directory.
move() method move Files in Python using the. The shutil. move() method takes two arguments first one is the complete source path and the second one is the destination path (including the file/folder name to move), the move function will move the file from source to the destination.
move() method Recursively moves a file or directory (source) to another location (destination) and returns the destination. If the destination directory already exists then src is moved inside that directory. If the destination already exists but is not a directory then it may be overwritten depending on os.
The problem is that you execute shell commands instead of actually changing the directory using os.chdir()
Each os.system()
call executes the given command in a new shell - so the script's working directory is not affected at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With