鎵归噺淇敼鏂囦欢鍚嶅瓧
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鎵归噺淇敼鏂囦欢鍚嶅瓧相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%89%b9%e9%87%8f%e4%bf%ae%e6%94%b9' title='鎵归噺淇敼'>鎵归噺淇敼
itext strong new txt spl 鑾峰彇 getc listdir淇敼浠绘剰鐩綍涓嬫枃浠跺悕瀛?/strong>
path_dir = r'鐩綍鍦板潃'
# 鑾峰彇鐩綍涓嬬殑鎵€鏈夋枃浠?a = os.listdir(path_dir)
n = 0
for i in a:
n += 1
x = str(n)
# 鍒嗗壊璺緞锛岃繑鍥炶矾寰勫悕鍜屾枃浠舵墿灞曞悕鐨勫厓缁? ext = os.path.splitext(i)
# 鍙慨鏀瑰悗缂€涓?txt鐨勬枃浠? if ext[1] == '.txt'
# 淇敼鍚庣殑鍚嶅瓧
new_name = x + ext[1]
# 瑕佷慨鏀圭殑璺緞涓嬬殑鏂囦欢
oldfile = os.path.join(path_dir, i)
# 淇敼鍚庡悕瀛楃殑璺緞
newfile = os.path.join(path_dir, new_name)
# 閲嶅懡鍚? os.rename(oldfile, newfile)
淇敼褰撳墠鐩綍涓嬫枃浠跺悕瀛?/strong>
# 褰撳墠鐩綍
path_dir = os.getcwd()
# 鑾峰彇鐩綍涓嬬殑鎵€鏈夋枃浠?a = os.listdir()
n = 0
for i in a:
n += 1
x = str(n)
# 鍒嗗壊璺緞锛岃繑鍥炶矾寰勫悕鍜屾枃浠舵墿灞曞悕鐨勫厓缁? ext = os.path.splitext(i)
# 鍙慨鏀瑰悗缂€涓?txt鐨勬枃浠? if ext[1] == '.txt':
# 淇敼鍚庣殑鍚嶅瓧
new_name = x + ext[1]
# 瑕佷慨鏀圭殑璺緞涓嬬殑鏂囦欢
oldfile = os.path.join(path_dir, i)
# 淇敼鍚庡悕瀛楃殑璺緞
newfile = os.path.join(path_dir, new_name)
# 閲嶅懡鍚? os.rename(oldfile, newfile)
以上是关于鎵归噺淇敼鏂囦欢鍚嶅瓧的主要内容,如果未能解决你的问题,请参考以下文章
windows涓嬪皢澶氫釜鏂囦欢鍚堝苟鎴愪竴涓枃浠讹紝灏唗s鏂囦欢鍙樻垚MP3鏍煎紡