我正在尝试在Windows 7中查找/Users/目录中的所有.mp3和.mp4文件。以下是我的代码。有什么想法吗?
import os
newpath = r'C:\Users\Media'
if not os.path.exists(newpath):
os.makedirs(newpath)
for root, dirs, files in os.walk("/Users"):
for file in files:
if file.endswith(".mp3"):
print(os.path.join(root, file))
os.rename(os.path.join(root, file), newpath)
for root, dirs, files in os.walk("/Users"):
for file in files:
if file.endswith(".mp4"):
print(os.path.join(root, file))
os.rename(os.path.join(root, file), newpath)
if file.endswith(".mp3") or file.endswith(".mp4")
。 如果文件以".mp3"或者".mp4"结尾,就执行下面的操作。 - Remi Guanglob.iglob(r'C:\ Users \ ** \ * .mp3',recursive = True)
的东西([文档](https://docs.python.org/3/library/glob.html))。 - Cristian Ciupitu