python 学习笔记一
上一篇 /
下一篇 2009-10-20 18:23:28
实例1:.实现自动生成TXT文本,此文本中记录所Python工作目录下的文件名称。
提示:此实例需要使用引用os模块与模块中的listdir()与getcwd()方法。
#coding=gbk
import os
txt=open("record.txt","w")
#os.getcwd()返回你工作目录中所有的文件名!
print 'current directory is',os.getcwd()
#print os.listdir('E:\\Python\Cases\Python')
print os.listdir(os.getcwd())
#os.getcwd()的作用是返回一个表示当前工作目录的字符串
#print os.rmdir('E:\\Python\Cases\Python\死心') #删除目录
print os.path.isdir('E:\\Python\Cases\Python')
print os.path.isfile('E:\\Python\Cases\Python\sanjiao.py')
实例2:在你的工作空间添加几个BMP格式的图片,使用Python将其名字改为00.bmp,01.bmp
,02.bmp……n.bmp 联系实例一,涉及的方法:使用os模块中的rename(oldName, newName)
#coding=gbk
import os
test = os.listdir(os.getcwd())
a=1
for i in test:
if i[-4:] == ".bmp":
file="%03i.bmp"%a
os.rename(i,file)
print i,file
a=a+1
#Tips:
#>>> print "Hello%03iWorld"%5
#Hello005World
import os
test = os.listdir(".")
i = 1
for file in test:
if file[-4:] == ".bmp":
print("test_%2s.bmp" % i)
i+=1
收藏
举报
TAG: