Hướng dẫn python terminal change directory
Changing the current directory of the script process is trivial. I think the question is actually how to change the current directory of the command window from which a python script is invoked, which is very difficult. A Bat script in Windows or a Bash script in a Bash shell can do this with an ordinary cd command because the shell itself is the interpreter. In both Windows and Linux Python is a program and no program can directly change its parent's environment. However the combination of a simple shell script with a Python script doing most of the hard stuff can achieve the desired result. For example, to make an extended cd command with traversal history for backward/forward/select revisit, I wrote a relatively complex Python script invoked by a simple bat script. The traversal list is stored in a file, with the target directory on the first line. When the python script returns, the bat script reads the first line of the file and makes it the argument to cd. The complete bat script (minus comments for brevity) is: Show Nội dung chính
The python script, dSup.py is:
A portable method of interacting with the operating system is offered through the OS Python Module. The module, which is a part of the default Python library, contains tools for locating and modifying the working
directory. The following contents are described in this article.
The __file__ function returns the path to the current script file (.py). Getting the current working directory- os.getcwd()The function os.getcwd() returns as a string str the absolute path to Python's current working directory. "Get current working directory" (getcwd) refers to the ability to print the current working directory with an operating system using print() and getcwd (). The trailing slash character is omitted from the string that is returned. ExampleFollowing is an example to get the current working directory:
OutputFollowing is an output of the above code: The Current working directory is: C:\Users\Lenovo\Desktopos.getcwd() returns an object of type: class 'str'> os.getcwd() returns an object of type: Change the current working directory: os.chdir()Use the chdir() function in Python to change the current working directory. The path to the directory you wish to change to is the only parameter the method allows. You can use either an absolute or relative path argument. ExampleFollowing is an example to change the current working directory:
OutputFollowing is an output of the above code: The Current working directory is: C:\Users\Lenovo\Desktop The Current working directory now is: C:\Users\Lenovo\Downloads\Works Note − A NotADirectoryError exception is thrown if a directory is not provided as an argument to the chdir() method. There is a FileNotFoundError exception raised if the provided directory is not found. A PermissionError exception is raised if the user running the script doesn't have the required permissions. Example
OutputFollowing is an output of the above example: The Current working directory is: C:\Users\Lenovo\Downloads\Works Updated on 17-Aug-2022 13:05:49
How do I change the directory of a file in Python?Set File Path in Python. Use the \ Character to Specify the File Path in Python.. Use the Raw String Literals to Specify the File Path in Python.. Use the os.path() Function to Specify the File Path in Python.. Use the pathlib.Path() Function to Specify the File Path in Python.. Which method is used to change the directory in Python?chdir() method in Python used to change the current working directory to specified path. How do you move to a different folder in Python?Steps to Move a File in Python. Find the path of a file. We can move a file using both relative path and absolute path. ... . Use the shutil.move() function. The shutil. ... . Use the os.listdir() and shutil move() function to move all files. Suppose you want to move all/multiple files from one directory to another, then use the os.. |