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!

12th Nov 2016, 3:04 AM
Alicia Evans-Morley
Alicia Evans-Morley - avatar
4 Antworten
+ 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)
12th Nov 2016, 5:11 AM
Sunera Avinash
Sunera Avinash - avatar
+ 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.
12th Nov 2016, 3:21 AM
Keto Z
Keto Z - avatar
+ 1
well the first answer I gave is the piece you need the second part is how to implement it in a for loop
12th Nov 2016, 2:01 PM
Sunera Avinash
Sunera Avinash - avatar
0
Thank you I will try these! will they both work in a loop?
12th Nov 2016, 11:46 AM
Alicia Evans-Morley
Alicia Evans-Morley - avatar