how to move file in python script

how to move file in python script

Hi,

In my Python program I have to move a file from one directory to another. What is the best way to move the file from one directory to another in Python?

Thanks

View Answers

April 18, 2021 at 9:13 AM

Hi,

You can use any of the following methods for moving a file to another directory in Python:

os.rename(), shutil.move(), or os.replace()

Here is the example program:

import os
import shutil

srcFile="/home/help/one.zip"
destFile="/home/help2/one.zip"

os.rename(srcFile, destFile)
shutil.move(srcFile, destFile)
os.replace(srcFile, destFile)

Thanks


April 18, 2021 at 9:14 AM

Hi,

You can find more examples at:

Thanks


July 18, 2021 at 6:09 AM

Hi,

I tested these examples of moving files in Python and its working fine.

Thanks

Ads









Related Tutorials/Questions & Answers:
how to move file in python script
how to move file in python script  Hi, In my Python program I have to move a file from one directory to another. What is the best way to move the file from one directory to another in Python? Thanks   Hi, You can use
How to move a file from a directory to other directory
How to move a file from a directory to other directory  I tried the above example it will move the folder to other directory but it does not move the image file or other file to a specified directory. Can anyone help me? Thanks
Advertisements
how to create a php script to download file
how to create a php script to download file  php script to download file
How to create a .mdf file from script (.sql)
How to create a .mdf file from script (.sql)  Hi, I have a sql database in 2008 server. now i want it to convert to 2005. so i script it. just help me to create a .mdf file back from the script
how to create a php script to download file from database mysql
how to create a php script to download file from database mysql  how to create a php script to download file from databse mysql
how to create a php script to download file from database mysql
how to create a php script to download file from database mysql  how to create a php script to download file from databse mysql
how to call java script function through java class file
how to call java script function through java class file  how to call jframe in java script
JavaScript move file
JavaScript move file...; In this section, we are going to move a file from one folder to another using... with the specified path. To move the file from the specified location to another location, we
How do I read a variable from a file in shell script
command. How do I read a variable from a file in shell script and then use...How do I read a variable from a file in shell script  Hi, In my project we are submitting the job on spark cluster through sh file. Now I want
Uploaded File Information in the Receiving Script?
Uploaded File Information in the Receiving Script?  How To Get the Uploaded File Information in the Receiving Script?   Hi friends, Once the Web server received the uploaded file, it will call the PHP script specified
How to move a value in array?
How to move a value in array?   Enter value at the End: 5 Display...]5 Index at [3]8 and when i enter 0 in last index it will move and if i enter again zero in last index it will not move anymore... pls help..thank you
How to move a value in array?
How to move a value in array?   Enter value at the End: 5 Display...]5 Index at [3]8 and when i enter 0 in last index it will move and if i enter again zero in last index it will not move anymore... pls help..thank you
how to move label in jframe in java?
how to move label in jframe in java?  please any one can give me code for moving a java label in Jframe
ModuleNotFoundError: No module named 'nowyou-grafis-script-python'
: No module named 'nowyou-grafis-script-python' How to remove the ModuleNotFoundError: No module named 'nowyou-grafis-script-python' error? Thanks...ModuleNotFoundError: No module named 'nowyou-grafis-script-python'  
ModuleNotFoundError: No module named 'nowyou-grafits-script-python'
: No module named 'nowyou-grafits-script-python' How to remove the ModuleNotFoundError: No module named 'nowyou-grafits-script-python' error...ModuleNotFoundError: No module named 'nowyou-grafits-script-python'  
ModuleNotFoundError: No module named 'python-script-converter'
: No module named 'python-script-converter' How to remove the ModuleNotFoundError: No module named 'python-script-converter' error? Thanks   ...ModuleNotFoundError: No module named 'python-script-converter'  Hi
ModuleNotFoundError: No module named 'utility-python-script'
: No module named 'utility-python-script' How to remove the ModuleNotFoundError: No module named 'utility-python-script' error? Thanks   Hi...ModuleNotFoundError: No module named 'utility-python-script'  Hi
ModuleNotFoundError: No module named 'utility-python-script'
: No module named 'utility-python-script' How to remove the ModuleNotFoundError: No module named 'utility-python-script' error? Thanks   Hi...ModuleNotFoundError: No module named 'utility-python-script'  Hi
ModuleNotFoundError: No module named 'nowyou-vstitcher-script-python'
: No module named 'nowyou-vstitcher-script-python' How to remove the ModuleNotFoundError: No module named 'nowyou-vstitcher-script-python' error...ModuleNotFoundError: No module named 'nowyou-vstitcher-script-python
How to Increase Script Execution Time Limit in php ?
How to Increase Script Execution Time Limit in php ?   Increase Script Execution Time Limit in php configuration   Sometimes you need... avoid this error by increasing script execution time limit in your PHP.INI file
how to move pointer in Textfield to newline
how to move pointer in Textfield to newline  consider my case and provide me solutions: import java.awt.; import java.awt.event.; class Table1 extends Frame implements ActionListener{ TextField tf1,tf2,tf3
How To Move Image Smoothly
How To Move Image Smoothly       In this example you will see how to move image smoothly on mouse motion using Java.  To move an image, we have used the Listener interface
how to solve 'Stop Running This Script' Error ?
how to solve 'Stop Running This Script' Error ?  Hi.. When opened the page on IE browser(version : 8.0),it gives an java script error (i.e.Stop Running This Script). I have used jsp/servlet for this. Please help to resolved
How to run PHP Script from the command line ?
How to run PHP Script from the command line ?  Running PHP Script from the command line   Actually it is very simple to run php script... expect. You can also check : http://www.phpzag.com/running-php-script-from
can i Execute PHP code inside python script?
can i Execute PHP code inside python script?    can i Execute PHP code inside python script
How to pretty print a JSON file in Python?
How to pretty print a JSON file in Python? In this tutorial I will teach you to read the JSON file in Python and then pretty print the content... file in Python, then use the Python function to format the JSON and print
ModuleNotFoundError: No module named 'move'
ModuleNotFoundError: No module named 'move'  Hi, My Python program...' How to remove the ModuleNotFoundError: No module named 'move' error... padas library. You can install move python with following command: pip
How to read properties file in Python using configparser?
How to read properties file in Python using configparser and printing the property value? In Python we can read the configuration file and then is required.... Following example shows you how to read the properties file in Python very easily
I am Java programmer. Should I learn Java Script or Python?
I am Java programmer. Should I learn Java Script or Python?  Hi, I learned Core Java and Advanced Java. What should I learn next? I am Java programmer. Should I learn Java Script or Python? Thanks   Hi, Actually
how to execute a unix shell script from a java program
how to execute a unix shell script from a java program  well , i am... solution . could anyone please give a sample code as of how to do this... what i am... the script name from a java code .once you execute this java program
how to get description for each file
how to get description for each file  hi, how to get description for each file using php or java script..... condtion:two text boxes for each file if the user select 10 file to upload........get the description

Ads