Skip to content
This repository has been archived by the owner on Jan 1, 2021. It is now read-only.
/ py-filename Public archive

A Python script to add/remove file name prefix/suffix

License

Notifications You must be signed in to change notification settings

iROCKBUNNY/py-filename

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

py-filename

A Python script to add/remove file name prefix/suffix

Core functions: filename.py

  1. filename.searchByExt(rootpath, ext) Find files in rootpath with a certain file extentison of ext, obtain their file paths, store those file paths into a list, and return it.

  2. filename.modifyPrefix(filelist, oldPrefix='', newPrefix='') For the files listed in filelist, change their file name prefix from oldPrefix to newPrefix.

  3. filename.modifySuffix(filelist, oldSuffix='', newSuffix='') For the files listed in filelist, change their file name suffix from oldSuffix to newSuffix.

Sample code: filename_sample_code.py

# -*- coding: utf-8 -*-

import filename

def main():
    rootpath = '~/Documents' # change this to a proper directory path
    ext = 'pdf' # change it to whatever file extension you need
    oldPrefix = '' # left empty when adding new prefix
    newPrefix = 'prefix-' # left empty when removing old prefix
    oldSuffix = '' # left empty when adding new suffix
    newSuffix = '-suffix' # left empty when removing old suffix

    results = filename.searchByExt(rootpath, ext)
    filename.modifyPrefix(results, oldPrefix, newPrefix)
    filename.modifySuffix(results, oldSuffix, newSuffix)

if __name__ == '__main__':
    main()

Further reading

  1. Python和正则表达式:给PDF文件批量添加前缀或后缀

About

A Python script to add/remove file name prefix/suffix

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages