shutil.move
时间: 2023-08-31 07:12:52 浏览: 108
shutil.move() is a Python function that is used to move files or folders from one location to another. It takes two arguments: the source file or directory path and the destination path. If the destination path does not exist, it will be created, and the file or directory will be moved there. If the destination path already exists, the file or directory will be overwritten.
Here is an example of how to use shutil.move() to move a file:
```python
import shutil
# move a file from one directory to another
source_file = '/home/user/documents/file.txt'
destination_file = '/home/user/new_folder/file.txt'
shutil.move(source_file, destination_file)
```
And here is an example of how to use shutil.move() to move a directory:
```python
import shutil
# move a directory from one location to another
source_dir = '/home/user/documents/my_folder'
destination_dir = '/home/user/new_folder/my_folder'
shutil.move(source_dir, destination_dir)
```
Note that if the source path is a directory, the entire directory tree will be moved to the destination path. If the destination path already exists and is a directory, the source directory will be moved into the destination directory. If the destination path already exists and is a file, an error will be raised.
阅读全文