move a json file in python

To move a JSON file in Python, we can use the os and shutil modules, which offers high-level operations on files and directories. Here's a code snippet that moves a JSON file from one directory to another:

main.py
import os
import shutil

# Set paths for your source and destination directories
src = '/path/to/source_directory/file.json'
dst = '/path/to/destination_directory/file.json'

# Use the shutil.move() method to move the file
shutil.move(src, dst)

# Check if the file has been successfully moved
if os.path.exists(dst):
    print(f"{src} has been successfully moved to {dst}")
375 chars
14 lines

In this code, we first define the source and destination paths as src and dst. Then we use the shutil.move() method to move the file from the source directory to the destination directory. Finally, we check if the file exists in the destination directory using the os.path.exists() method, and print a success message if it does.

gistlibby LogSnag