用到了os的两个接口:
1、列出文件夹中的所有文件(也包含目录)
os.listdir(path)
Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries ‘.’ and ‘..’ even if they are present in the directory.Availability: Unix, Windows.
Changed in version 2.3: On Windows NT/2k/XP and Unix, if path is a Unicode object, the result will be a list of Unicode objects. Undecodable filenames will still be returned as string objects
2、对文件进行重命名
os.rename(src, dst)
Rename the file or directory src to dst. If dst is a directory, OSError will be raised. On Unix, if dst exists and is a file, it will be replaced silently if the user has permission. The operation may fail on some Unix flavors if src and dst are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement). On Windows, if dst already exists, OSError will be raised even if it is a file; there may be no way to implement an atomic rename when dst names an existing file.Availability: Unix, Windows
import os
dirpath = "D:/workbench/crazyant.net/myfiles"
for fname in os . listdir ( dirpath ) :
newfname = fname [ 3 : ]
newfpath = "%s/%s" % ( dirpath , newfname )
oldfpath = "%s/%s" % ( dirpath , fname )
os . rename ( oldfpath , newfpath )
其实就是用os.listdir读取里面所有的文件,然后用os.rename进行文件重命名即可实现。
python的os模块官方介绍:http://docs.python.org/2/library/os.html