[PYTHON] Rename and replace remote files using NcFTP in the shell

Rename remote files with NcFTP


#!/bin/bash

echo 'open -u ${USER} -p ${PASS} ${HOST}
cd ${BASE_DIR}
ls
rm -rf ${BACKUP_FILE}
rename ${CURRENT_FILE_NAME} ${BACKUP_FILE}
rename ${NEW_FILE_NAME} ${CURRENT_FILE_NAME}
ls
quit'|ncftp

When switching files, I wrote it with the intention of hitting it from Jenkins, but in the end I rejected it and changed to python below.

Rename remote files in Python


#!/usr/bin/env python
# -*- coding: utf-8 -*-

print 'python >>>'

import sys, os, datetime, re
from ftplib import FTP

ftp_host = 'hostname'
ftp_user = 'username'
ftp_pass = 'password'
ftp_base_dir = 'Directory with files to rename'
ftp_from_dir = 'Newly published file'
ftp_to_dir = 'Currently open files'
ftp_backup_prefix = '_'

def main(arg_user='', arg_pass='', *args):

	if arg_user == '' or arg_pass == '':
		print '  Rename directory failed'
		print '    Error: User or Password is not input.'
		return

	ftp_user = arg_user
	ftp_pass = arg_pass
	
	dt = str(datetime.datetime.utcnow())
	dt = dt.replace(':', '-')
	dt = dt.replace(' ', '_')
	dt = re.sub(r'\.\d+', '', dt)

	print '  Rename directory start >>> ' + dt

	try:
		ftp = FTP(ftp_host)
		ftp.login(ftp_user,ftp_pass)
		ftp.cwd(ftp_base_dir)
		file_list = ftp.nlst()
		if ftp_from_dir in file_list:
			ftp.rename(ftp_to_dir, ftp_backup_prefix+dt)
			ftp.rename(ftp_from_dir,ftp_to_dir)
		else:
			print '  Rename directory failed'
			print '    Error: NotFound New Directory'
		ftp.close()
		print '  Rename directory finished'

	except Exception as e:
		print '  Rename directory failed'
		print '    Error: ' + e.message


if __name__ == '__main__':
	sys.path.append(os.path.dirname(__file__))
	sys_args = sys.argv
	args = sys_args[1:] if len(sys_args) > 1 else []
	main(*args)

Run python above


python file_rename.py UserName Password

Backup should be done at the time of upload I feel that it is okay to delete old files.

Recommended Posts

Rename and replace remote files using NcFTP in the shell
Problems and solutions for using the remote debugging environment for Linux apps in Visual Studio 2017
Using verticalenv in shell scripts
Process the files in the folder in order with a shell script
[Shell] How to get the remote default branch in Git
Regularly upload files to Google Drive using the Google Drive API in Python
Export and output files in Python
Replace the directory name and the file name in the directory together with a Linux command.
Load the remote Python SDK in IntelliJ
Notes using cChardet and python3-chardet in Python 3.3.1.
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Try using the HL band in order
Tweet using the Twitter API in Python
Read all csv files in the folder
Read and write JSON files in Python
Easily graph data in shell and Python
Find it in the procession and edit it
Download files in any format using Python
Add PATH when installing Anaconda3 in the terminal Change shell and hide (base)
Replace the named entity in the read text file with a label (using GiNZA)