Python Memorandum: Refer to the text and edit the file name while copying the target file

Get the file name

Since the target file was path information, only the file name is acquired in advance.

py:os.path.basename


file_name = os.path.basename(txt_fle_line)[:-1]

XCOPY I also tried using XCOPY temporarily with "subprocess.call", I also needed to move, so I decided to use shutil for versatility.

subprocess.call


import subprocess
subprocess.call("xcopy %s %s /D /K /R /Y /C /V /E"%(file_path, send))

Update time

Since we adopted the new one at the time of update, we took time and compared it.

shutil.py


        if os.path.exists(rename_file) :

            copy_date = os.stat(rename_file).st_mtime
            org_date = os.stat(file_path).st_mtime

            if copy_date < org_date:
                print "%s copy... %s" %(send, grid_name)
                shutil.copy2(file_path, rename_file)

        else:

A script that edits the file name while copying the target file by referring to the text.

copy_target.py


import os
import shutil
from datetime import datetime as dt

txt_file = open(r"copy_target.txt","r")

for target_line in txt_file:

    if target_line == '\n':
        break

    target_cl = target_line[:-1].split(',')

    file_path = target_cl[0].strip('"')
    file_name = target_cl[1].strip('"')

    grid_cl = file_name.split('_')

    if len(grid_cl) == 6:

        photo = grid_cl[0]
        area = grid_cl[1]
        b_cl = grid_cl[2]
        b_rw = grid_cl[3]
        b_no = grid_cl[4]
        flg = grid_cl[5]

    grid_name = area + b_cl + b_rw
    send = r'G:\grid\\' + area + '\\' + grid_name

    if os.path.isdir(send) :

        rename_file = send +'\\'+ file_name

        if os.path.exists(rename_file) :

            copy_date = os.stat(rename_file).st_mtime
            org_date = os.stat(file_path).st_mtime

            if copy_date < org_date:
                print "%s copy... %s" %(send, grid_name)
                shutil.copy2(file_path, rename_file)

        else:
            print "%s copy new... %s" %(send, grid_name)
            shutil.copy2(file_path, rename_file)

    else:
        is_not_file = open(r"D:\python\file_serch\target_work\is_not_GRID.txt", "a+")
        is_not_file.write(file_path + "," + file_name + "," + "no path" + "\n")

        is_not_file.close()

Recommended Posts

Python Memorandum: Refer to the text and edit the file name while copying the target file
The file name was bad in Python and I was addicted to import
[python] Change the image file name to a serial number
Try to decipher the garbled attachment file name with Python
[Python scraping] Output the URL and title of the site containing a specific keyword to a text file
[Python Kivy] How to get the file path by dragging and dropping
Python / subprocess> Symbolic link Implementation to get only the destination file name> os.readlink ()
Attempt to launch another .exe and save the console output to a text file
Output python log to both console and file
Memorandum (in openpyxl ① copy and paste from another book ② refer to the comparison table)
I want to see the file name from DataLoader
Download the file while viewing the progress in Python 3.x
How to get the variable name itself in python
Template of python script to read the contents of the file
Automatically determine and process the encoding of the text file
A memorandum to run a python script in a bat file
Read the xml file by referring to the Python tutorial
Wrap and encapsulate the validation target to prevent complications
Python program that looks for the same file name
Python OpenCV tried to display the image in text.
How to read text by standard input or file name specification like cat in Python
Python --Read data from a numeric data file to find the covariance matrix, eigenvalues, and eigenvectors
Jedi-vim shortcut command that allows you to refer to the definition source and definition destination in Python
It was great to edit the Python file in the Raspberry Pi with Atom's remote function
Edit the config file and run docker-compose up (Django + MySQL ④)
How to switch the configuration file to be read by Python
Change the standard output destination to a file in Python
[Python] Read the csv file and display the figure with matplotlib
Get the MIME type in Python and determine the file format
[Introduction to Udemy Python3 + Application] 39. while statement, continue statement and break statement
Build a Python environment and transfer data to the server
I want to know the features of Python and pip
I tried to enumerate the differences between java and python
I tried to divide the file into folders with Python
Convert Excel file to text in Python for diff purposes
Process the gzip file UNLOADed with Redshift with Python of Lambda, gzip it again and upload it to S3
Get the list in the S3 bucket with Python and search with a specific Key. Output the Key name, last update date, and count number to a file.