0
Removing spaces and dashes front filenames
I'm running code to remove dashes and spaces from code with a specific extension (.dwg). how do I nest a statement to find files in a loop then remove dashes and spaces? please help! thank you much!
4 Respostas
+ 2
import re
filename=re.sub(r"(-| )","",filename)
if you want the full solution or something I think the code given below will be OK
however use try...except to handle errors
import re
import os
# path
os.chdir("storage/sdcard0/org.qpython.qpy/files/dice")
files=os.listdir()
for filename in files:
# extension
# would be better if enclosed in a try ... except block
if filename[-4:] == ".dwg":
# changing file name
filename2=re.sub(r"(-| )","",filename)
if filename != filename2:
os.rename(filename,filename2)
print("Fixed:", filename)
+ 1
for char in text:
if char == " " or char == "-":
char = ""
That would be my best guess. Basically, check every character, and if it is a space or hyphen, then make it nothing.
+ 1
well the first answer I gave is the piece you need
the second part is how to implement it in a for loop
0
Thank you I will try these! will they both work in a loop?