Python, copy file with creation

python os copy file
python copy file and rename
python delete file
python copy all files in directory
shutil move
python move file
python copy file overwrite
python script to copy files from one server to another

I'm writing script on Python for copy cron config. I need to copy my file to /etc/cron.d/, and if destination file isn't exist it must be created. I found solution, but it doesn't provide missing file, here it is:

from shutil import copyfile


def index():
    src = "/opt/stat/stat_cron"
    dst = "/etc/cron.d/stat_cron"
    copyfile(src, dst)


if __name__ == "__main__":
    index()

I get exception "FileNotFoundError: [Errno 2] No such file or directory: '/etc/cron.d/stat_cron'"

Please, tell me correct solution.


from pathlib import Path

def index():
    src = "/opt/stat/stat_cron"
    dst = "/etc/cron.d/stat_cron"
    my_file = Path(dst)
    try:
        copyfile(src, dest)
    except IOError as e:
        my_file.touch()       #create file
        copyfile(src, dst)

Use pathlib to check if file exists and create a file if not.

shutil — High-level file operations — Python 3.8.5 documentation, The shutil module offers a number of high-level operations on files and Other metadata, like the file's creation and modification times, is not preserved. Python Copy File Methods. Python provides in-built functions for easily copying files using the Operating System Shell utilities. Following command is used to Copy File shutil.copy(src,dst) Following command is used to Copy File with MetaData Information shutil.copystat(src,dst) Here are the steps to copy file in Python


using os.makedirs can help to check the condition if file exist and create one if not

from shutil import copyfile
import os

def index():
    src = "/opt/stat/stat_cron"
    dst = "/etc/cron.d/stat_cron"
    os.makedirs(dst,exit_ok=True)
    copyfile(src, dst)


if __name__ == "__main__":
    index()

10.10. shutil — High-level file operations — Python 2.7.18 , If dst is a directory, a file with the same basename as src is created (or overwritten ) in the directory specified. Permission bits are copied. src and dst are path� However, you may have noticed the creation date was preserved on the new file, unlike with shutil.copy, which copied "file1.txt" to "file3.txt" and gave it a new creation date. copyfileobj This method copies the content of a source file into a destination file, from the current source-file position.


everybody thanks. success solved issue with next coditions:

out_file_exists = os.path.isfile(dst)
out_dir_exists = os.path.isdir("/etc/cron.d")

if out_dir_exists is False:
    os.mkdir("/etc/cron.d")

if out_file_exists is False:
    open(dst, "a").close()

How to Copy a File in Python, When it comes to using Python to copy files, there are two main ways: using the shutil then instead of copying the file, a new symbolic link will be created. But in our case we already have the file, so we are not required to create a new file. Step 2) for i in range(2): f.write("Appended line %d\r " % (i+1)) This will write data into the file in append mode. You can see the output in "guru99.txt" file. The output of the code is that earlier file is appended with new data.


Python COPY File using shutil.copy(), shutil.copystat(), Python Copy File Methods. Step 1) Before, we copy a file, we need to get the the path to the original file in the current directory. Code Explanation. Step 2) We use Shutil Module to create a copy of the existing file. Code Explanation. Step 3) Copy function only copies the content of the file but no other information. Python Exercises, Practice and Solution: Write a Python program to get file creation and modification date/times.


How to Do Python Copy File - 9 Ways for Beginners, shutil copy2() method. However, the copy2() method functions like the copy(). But it also gets the access and modification times added in the meta-data while� Move the file with new name; Copy and rename file using "os" module; Move and Rename file shutil.move(src, dst, copy_function=copy2) Listing command:-bash-4.2$ ls python_samples test test.txt test.txt.copy test.txt.copy2 Code:


Python, copyfile() method in Python is used to copy the content of source file to destination file. Metadata of the file is not copied. Source and destination must represent a file and destination must be writable. If destination already exists then it will be replaced with the source file otherwise a new file will be created. I have the situation whereby I want to keep the original attributes on a file (the file creation date etc). Normally when you copy files in Windows, the copy that you make gets new 'modified' dates etc. I have come accross the shutil.copy command — although this doesn't keep the file attributes the same.