Python batch rename objects randomly

Source: Internet
Author: User

Recently, the project is relatively tight, and the project has entered a later stage. The test is too busy. It was originally a task of testers and I was a new employee. So the boss handed me a task, it is to check whether there is memory leakage in a project developed on the Win8 platform, and use Windows's built-in perfmon. MSC monitors and records the monitoring data at the same time. Because there are not many test cases, you need to copy the same test cases multiple times and put them in the same directory.ProgramThe background is opened repeatedly. However, in Windows 8, if you perform multiple copies on the same file, the file name is simply added with a copy (1) after the original file. However, if so, files of the same size are arranged together. The sorting function of Windows 8 cannot sort files in disorder, will cause errors to program monitoring. If you use a program to open a file when a series of large files are together, the memory will fluctuate. This kind of floating will be unstable and cause errors in the test, which is intolerable. Therefore, the best way is to rename all the files and name them randomly. Then, the sorting can be disrupted in this way, and then the test is ongoing.

The program demo is as follows:

 #  Renamerand. py  #  Yanggd 2012/9/7  #  Get file name, and then rename the file  # After renamed, the name likes this: [A-Za-Z] [A-Za-Z] [0-9] [0-9] [A-Za-Z] [0-9]  Import  Random  Import  Osexistname = []  Def  Renamerand (PATH): filelist = OS. listdir (PATH) #  List the file name Rangeletter = ( '  Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz  ' )  For Oldfilenameandext In  Filelist: newfilename = Random. Choice (rangeletter) + random. Choice (rangeletter) + \ STR (random. randrange ( 10) + STR (random. randrange (10) + random. Choice (rangeletter) + STR (random. randrange (10 ))  While Newfilename. Upper () In  Existname: newfilename = Random. Choice (rangeletter) + random. Choice (rangeletter) + \ STR (random. randrange ( 10) + STR (random. randrange (10) + random. Choice (rangeletter) + STR (random. randrange (10 ) Existname. append (newfilename. Upper () [oldfilename, fileext] = Oldfilenameandext. Split ( '  .  ' , 1 ) Newfilenameandext = [Newfilename, fileext] newfilenameandext = '  .  '  . Join (newfilenameandext) path = OS. Path. abspath (PATH) pathsrc = Path + '  \\  ' + Oldfilenameandext pathdst = Path + '  \\  ' + Newfilenameandext OS. Rename (pathsrc, pathdst)  If   _ Name __ = '  _ Main __  '  : Renamerand (R  ' F: \ practice code \ testdata \ rename  ' )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.