Reputation: 319
I am trying to find all of the .mp3 and .mp4 files in my /Users/ directory in windows 7. Here is my code is bellow. An ideas on what to do??
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)
Upvotes: 3
Views: 761
Reputation: 560
Your code is pretty much correct, but the only thing is that, In the code given above there is a tab before first if
statement, but that is not required in any sense. That's why you are getting such an error. Please remove that tab or indentation so as to solve the problem. The corrected code will look like as follows:
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)
Upvotes: 1