1.python读取文件操作 与函数返回值:
def getFilePath():
try:
f = open("C:\\Users\\tangjing\\Desktop\\test.py",'r')
print(f.readline())
finally:
return f;
myFile = getFilePath()
print(myFile.name)
myFile.close()
2.列举路径下所有文件:
while True:
path = input('请输入路径:').strip()
if os.path.isdir(path) == True:
break
for i in os.listdir(path):
if os.path.isdir(path+"\\"+i) == True:
print("文件夹"+path+"\\"+i)
else:
f = open(path+"\\"+i,"r")
print("文件"+f.name)
3.安装第三方库
4.使用mp3play播放音乐
4.1python3经试验用mp3play库播放mp3不成功
mp3File = "E:\\lol\\qsbl.mp3"
clip = mp3play.load(mp3File)
clip.play()
duration = clip.milliseconds() #返回mp3的时长 毫秒
time.sleep(duration)
clip.stop()
4.2使用pygame播放音乐(成功播音乐)
mp3File = "E:\\lol\\qsbl.mp3"
pygame.mixer.init()
track = pygame.mixer.music.load(mp3File)
pygame.mixer.music.play()
time.sleep(20000)
pygame.mixer.music.stop()
4.3列出文件夹下的mp3音乐文件
list = os.listdir("E:\KuGou")
counter = 0
while counter < len(list):
if os.path.splitext(list[counter])[1]==".mp3":
print(list[counter])
counter +=1