How to use 'python move file' in Python

Every line of 'python move file' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
32def move_file(file_name, new_name):
33
34 print("Moving File '{}' to '{}'".format(file_name, new_name))
35
36 # Try to rename the file
37 try:
38 os.rename(file_name, new_name)
39 except OSError as e:
40 print("Error moving file")
41 print(e)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

464def move_file(src, dest):
465 """Move source file to destination.
466
467 Overwrites dest.
468
469 Args:
470 src: str or path-like. source file
471 dest: str or path-like. destination file
472
473 Returns:
474 None.
475
476 Raises:
477 FileNotFoundError: out path parent doesn't exist.
478 OSError: if any IO operations go wrong.
479
480 """
481 try:
482 os.replace(src, dest)
483 except Exception as ex_replace:
484 logger.error("error moving file %s to "
485 "%s. %s", src, dest, ex_replace)
486 raise

Related snippets