Use Python batch to modify file names in a specific string

xiaoxiao2021-03-06  37

The rename command under Windows can only use the same length string to replace the specific characters in the file name. The MV under CygWin does not support bulk renames. So write one yourself. It turned out to use C, it feels too much; mainly thinks more to use Python. The first time I use Python when I do Video Encoder, treat it as a supercuptizer. Write some small programs to run some algorithms, such as RGB to YUV, FDCT, IDCT. Unfortunately, I stayed in the company's computer when I left. This problem is very simple. Although I have written a few Python's applet, most of them are some pieces (the pieces can run, it is a reason for my fascination).

#! / usr / bin / env python

Import sys, string, OS

For root, dir, files in os.walk (sys.argv [1]): for file in file: newname = string.join (String.Split (file, sys.argv [2]), sys.argv [3] ) Newpath = sys.argv [1] newname oldpath = sys.argv [1] file try: Os.Rename (OldPath, newPath) Except valueerror: print "error when rename the file" OldPath Except Namerror: Print "error" error "error When rename the file " OldPath Except Oserror: Print NewPath " The File IS Already Exist! "

This is actually only a piece. But no matter what, it works. How to use is:

RN.PY PATH SRC_STRING DST_STRING

For example, in Cygwin:

[Williamx @ William3 Test] $ LS

789.tt ok789s.t ok789s1.t ok789s1.t2 rn.py rn.py ~

[Williamx @ William3 Test] $ rn.py ./789 456

[Williamx @ William3 Test] $ LS

456.tt ok456s.t ok456s1.t ok456s1.t2 rn.py rn.py ~

[Williamx @ William3 Test] $

I hope to be useful to you.

转载请注明原文地址:https://www.9cbs.com/read-64010.html

New Post(0)