A memo organized by renaming the file names in the folder with python

I really wanted to write with one liner, but I couldn't. I didn't understand how to rename mac because it seems to behave differently from Linux.

The example sentence assumed this time is to rename only the jpg image in the swimsuit image folder.

rename.py


#!/usr/bin/env python
# coding: UTF-8
import sys
import os
import re
a = 1
#Image folder to specify
files = os.listdir('/Users/hage/Pictures/sukumizu/')
for file in files:
    jpg = re.compile("jpg")
    if jpg.search(file):
        os.rename(file, "image%06d.jpg " %(a))
        a+=1
    else:
        pass

This time, I will roughly target only jpg images. Set search to sukima.

If you use this above code, please check the operation for the time being. We are not responsible for phenomena such as the image disappearing regardless of whether it is confirmed or not.

Probably like this when testing

touch vip.jpg sukima.jpg futaba.jpg ookami.jpg


 Please prepare a dummy file in a disposable folder without images and check the behavior.


### Image transfer with SCP
 Image transfer from scp command in python


#### **`o.py`**
```python

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

import os,sys,re,shutil,pexpect,datetime

s = 'Forwarding server'
u = 'Login user name'
d = 'Destination directory'
r = 'Image storage location starting with http at the transfer destination'
#Basically, the purpose is to raise SS on the spot, so Desktop is the case at home.
p = 'Image storage location of the transfer source'
t = datetime.datetime.now()
#Because SS is a name that starts with Scr
i = re.compile("^Scr")

#Preparing to give a time stamp
stamp = "{0:02d}".format(t.month) + "{0:02d}".format(t.day) + \
        "{0:02d}".format(t.hour) + "{0:02d}".format(t.minute)
#print stamp
files = os.listdir('/Directory with images/')
for file in files:
    if i.search(file):
        obj = file
obj = p+obj

shutil.copyfile(obj, '%simg%s.png' % (p,stamp))
scp=pexpect.spawn('scp -p %simg%s.png %s@%s:%s' %(p,stamp, u,s, d))
scp.interact()
#Output image save destination
print r + 'img%s.png' % stamp

What's missing is pip or easy-install. When raising SS as much as possible, I tried to raise it in one place (preferably in a place under my control) without distributing it.

It's inconvenient to upload images here and there when it catches fire on SNS! !! !!

Recommended Posts

A memo organized by renaming the file names in the folder with python
[Python] Get the files in a folder with Python
[Python] Open the csv file in the folder specified by pandas
Read the file line by line in Python
Read the file line by line in Python
Sort the file names obtained by Python glob in numerical order
Read a file in Python with a relative path from the program
Read line by line from a file with Python
I tried to open the latest data of the Excel file managed by date in the folder with Python
How to drop Google Docs in one folder in a .txt file with python
I tried searching for files under the folder with Python by file name
Get the file name in a folder using glob
A memo that I touched the Datastore with python
Run a Python file with relative import in PyCharm
Download the file in Python
A memo that implements the job of loading a GCS file into BigQuery in Python
A memo when checking whether the specified key exists in the defined dictionary with python
[Python] Create a file & folder path specification screen with tkinter
Change the standard output destination to a file in Python
Process the files in the folder in order with a shell script
[Note] Import of a file in the parent directory in Python
Extract the xz file with python
File / folder path manipulation in Python
Save the binary file in Python
I made a class to get the analysis result by MeCab in ndarray with python
Create a binary file in Python
Recursively get the Excel list in a specific folder with python and write it to Excel.
Python memo ① Folder and file operations
Create a docx file with thumbnails of photos in the folder pasted with the Python python-docx library and the free software "Reduction only."
[Python] A memo of frequently used phrases (by myself) in Python scripts
Process the contents of the file in order with a shell script
Output the contents of ~ .xlsx in the folder to HTML with Python
Read the standard output of a subprocess line by line in Python
[Python] A memo to operate ROM created by GBDK with PyBoy
The idea of feeding the config file with a python file instead of yaml
Get the formula in an excel file as a string in Python
Solve the subset sum problem with a full search in Python
Get a list of files in a folder with python without a path
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Exclusive control with lock file in Python
Write the test in a python docstring
Check the existence of the file with python
Display Python 3 in the browser with MAMP
A memo with Python2.7 and Python3 on CentOS
Search the maze with the python A * algorithm
Let's read the RINEX file with Python ①
Run the Python interpreter in a script
[GPS] Create a kml file in Python
Organize data divided by folder with Python
[Python] Read the specified line in the file
I made a configuration file with Python
Replace the directory name and the file name in the directory together with a Linux command.
Extract lines that match the conditions from a text file with python
Read a Python # .txt file for a super beginner in Python with a working .py
Download the file by specifying the download destination with Python & Selemiun & Chrome (Windows version)
Test & Debug Tips: Create a file of the specified size in Python
A memo corresponding to Django's runserver moss in Python 2.7.11 entered with Homebrew
Receive a list of the results of parallel processing in Python with starmap
Create a record with attachments in KINTONE using the Python requests module
I made a program to check the size of a file in Python
[Implementation example] Read the file line by line with Cython (Python) from the last line