python recursively rename directories - python

I made a script to rename directories with a name that contains spaces or special characters recursively:
import os
import re
import pdb
def renameInvalid(root):
print("root is: " + root)
for f in os.listdir(root):
if os.path.isdir(f):
old = f
f = f.replace(" ", "_")
f = re.sub(r'[^a-zA-Z0-9-_]', '',f)
if old != f:
print(root + " na substitutie")
os.rename(old,f)
print(root + " na hernoemen")
print("renamed " + old + " to " + f )
#pdb.set_trace()
f = '/' + f
pad = root + f
renameInvalid(str(pad))
mountpunt = os.getcwd()
renameInvalid(mountpunt)
You can test this script by making two directories with names containing spaces. You place one of the directories inside the other and run the script from inside the first directory. The script renames the first directory but generates an OSError on isdir(f).
Does anyone know what is the problem here?
Regards,

I found the answer (thanks to timbaileyjones for his solution).
import os
import re
def renameInvalid(root):
for f in os.listdir(root):
old = f
f = f.replace(" ", "_")
f = re.sub(r'[^a-zA-Z0-9-_]', '',f)
if old != f:
os.rename(old,f)
print("renamed " + old + " to " + f )
if os.path.isdir(f):
os.chdir(f)
renameInvalid(".")
os.chdir("..")
renameInvalid(".")
One should only run this code if they know what they are doing. It renames all the folders and files with whitespace or special characters in the filename.
Regards,

Related

Rename a file that already exists

I'm learning python and also english. And I have a problem that might be easy, but I can't solve it. I have a folder of .txt's, I was able to extract by regular expression a sequence of numbers of each one. I rename each file with the sequence I extracted from .txt
path_txt = (r'''C:\Users\user\Desktop\Doc_Classifier\TXT''')
for TXT in name_files3:
with open(path_txt + '\\' + TXT, "r") as content:
search = re.search(r'(([0-9]{4})(/)(([1][9][0-9][0-9])|([2][0-9][0-9][0-9])))', content.read())
if search is not None:
name3 = search.group(0)
name3 = name3.replace("/", "")
os.rename(os.path.join(path_txt, TXT),
os.path.join("Processos3", name3 + "_" + str(random.randint(100, 999)) + ".txt"))
I need to check if the file already exists, and rename it by adding an increment. Currently to differentiate the files I am adding a random number to the name (random.randint(100, 999))
PS: Currently the script finds "7526/2016" in .txt, by regular expression. Remove the "/". Rename the file with "75262016" + a random number (example: 7526016_111). Instead of renaming using a random number, I would like to check if the file already exists, and rename it using an increment (example: 7526016_copy1, 7526016_copy2)
Replace:
os.rename(
os.path.join(path_txt, TXT),
os.path.join("Processos3", name3 + "_" + str(random.randint(100, 999)) + ".txt")
)
With:
fp = os.path.join("Processos3", name3 + "_%d.txt")
postfix = 0
while os.path.exists(fp % postfix):
postfix += 1
os.rename(
os.path.join(path_txt, TXT),
fp % postfix
)
The code below iterates through the files found in the current working directory, and looks a base filename and for its increments. As soon as it finds an unused increment, it opens a file with that name and writes to it. So if you already have the files "foo.txt", "foo1.txt", and "foo2.txt", the code will make a new file named "foo3.txt".
import os
filenames = os.listdir()
our_filename = "foo"
cur = 0
cur_filename = "foo"
extension = ".txt"
while(True):
if (cur_filename) in filenames:
cur += 1
cur_filename = our_filename + str(cur) + extension
else:
# found a filename that doesn't exist
f = open(cur_filename,'w')
f.write(stuff)
f.close()

Check for files in 3 separate folders and rename if exist with filename(1), filename(2)

Task 1: I've completed.
Write a python script to generate three sets of 50 files (150 files total) with random dates in the file name from within the last 50 days:
IT421_unixdate.txt, IT151_unixdate.txt, IT620_unixdate.txt
Task 2:
Write a python script to:
Check if the folders IT421, IT151, IT620 exist, if they do not, create them. Move the files that begin with ITx to the folder ITx
If the file already exists in the folder, rename it like the following example:
IT421_1456991403.txt becomes IT421_1456991403(1).txt, IT421_1456991403(1).txt becomes IT421_1456991403(2).txt, and so on….
I am stuck on task2 part 2
#!/usr/local/bin/python3
import random
import time
import os
folders = ["IT421", "IT151", "IT620"]
#task2(1)
for i in range(0,len(folders)):
if not os.path.exists(folders[i]):
os.mkdir(folders[i])
#task1
for i in range (0,50):
for classIT in folders:
pastFiftyDays = int(time.time()) - 432000
currentTime = int(time.time())
timeStamp = str(random.randrange(pastFiftyDays, currentTime))
#Task2(2)
if classIT == "IT151":
open('IT151/' + classIT + "_" + timeStamp + .txt".format(classIT), "w")
elif classIT == "IT421":
open('IT421/' + classIT + "_" + timeStamp + ".txt".format(classIT), "w")
else:
open('IT620/' + classIT + "_" + timeStamp + ".txt".format(classIT), "w")
This is somewhere along the lines of what I've been trying.
#!/usr/local/bin/python3
import os
import fnmatch
folders = ["file_a", "file_z", "file_c"]
for i in range(0,len(folders)):
x = folders[i]
for filename in os.listdir('./temp'):
if fnmatch.fnmatch(filename, folders[i] + '.txt'):
open('temp/' + x + "(1).txt".format(x), "w")
else:
open('temp/' + x + ".txt".format(x), "w")
I am new to python and have been working and searching for days for ways to solve this problem. Any help would be appreciated. Thank you.

How to decompress zip files across a Windows folder in Python

I have a large folder having 900+ sub-folders, each of which has another folder inside it which in turn has a zipped file.
Its like -
-MyFolder
-----MySubfolder
---------MySubSubfolder
-------------MyFile.zip
How can I decompress all the zipped files in their respective folder OR in a separate folder elsewhere in Windows using Python?
Any help would be great!!
You could try something like:
import zipfile,os;
def unzip(source_filename, dest_dir):
with zipfile.ZipFile(source_filename) as zf:
for member in zf.infolist():
extract_allowed = True;
path = dest_dir;
words = member.filename.split('/');
for word in words:
if (word == '..'):
extract_allowed = False;
break;
if (extract_allowed == True):
zf.extract(member, dest_dir);
def unzipFiles(dest_dir):
for file in os.listdir(dest_dir):
if (os.path.isdir(dest_dir + '/' + file)):
return unzipFiles(dest_dir + '/' + file);
if file.endswith(".zip"):
print 'Found file: "' + file + '" in "' + dest_dir + '" - extracting';
unzip(dest_dir + '/' + file, dest_dir + '/');
unzipFiles('./MyFolder');

How to take care of duplicates while copying files to a folder in python

I am writing a script in python to consolidate images in different folders to a single folder. There is a possibility of multiple image files with same names. How to handle this in python? I need to rename those with "image_name_0001", "image_name_0002" like this.
You can maintain a dict with count of a names that have been seen so far and then use os.rename() to rename the file to this new name.
for example:
dic = {}
list_of_files = ["a","a","b","c","b","d","a"]
for f in list_of_files:
if f in dic:
dic[f] += 1
new_name = "{0}_{1:03d}".format(f,dic[f])
print new_name
else:
dic[f] = 0
print f
Output:
a
a_001
b
c
b_001
d
a_002
If you have the root filename i.e name = 'image_name', the extension, extension = '.jpg' and the path to the output folder, path, you can do:
*for each file*:
moved = 0
num = 0
if os.path.exists(path + name + ext):
while moved == 0:
num++
modifier = '_00'+str(num)
if not os.path.exists(path + name + modifier + extension):
*MOVE FILE HERE using (path + name + modifier + extension)*
moved = 1
else:
*MOVE FILE HERE using (path + name + ext)*
There are obviously a couple of bits of pseudocode in there but you should get the gist

I am using your ftputil within a python script

I am using your ftputil within a python script to get last modification/creation date of files in directory and I am having few problems and wondered if you could help.
host.stat_cache.resize(200000)
recursive = host.walk(directory, topdown=True, onerror=None)
for root,dirs,files in recursive:
for name in files:
#mctime = host.stat(name).mtime
print name
The above outputs a listing of all files in the directory
host.stat_cache.resize(200000)
recursive = host.walk(directory, topdown=True, onerror=None)
for root,dirs,files in recursive:
for name in files:
if host.path.isfile("name"):
mtime1 = host.stat("name")
mtime2 = host.stat("name").mtime
#if crtime < now -30 * 86400:
#print name + " Was Created " + " " + crtime + " " + mtime
print name + " Was Created " + " " + " " + mtime1 + " " + mtime2
Above produces no output
You've put name in quotes. So Python will always be checking for the literal filename "name", which presumably doesn't exist. You mean:
if host.path.isfile(name):
mtime1 = host.stat(name)
mtime2 = host.stat(name).mtime

Categories