Python 27 renaming multiple CAD Files - python

I have several (n=1,030) CAD drawing files (.dwg) spread across 51 subdirectories that have the following file naming convention:
(a) 0000-0n-0n.dwg
which needs to be changed to:
(b) _0000_0n_0n.dwg
The original file names (a) comprise three strings, each separated by dashes (-), namely:
a fixed four numeral prefix, followed by n > 1 alphanumeric, then another n> 1 alphanumeric, ending with the .dwg file extension.
The renamed files (b) should preserve these three strings described above,
but prefix the file name with an underscore and replace the current dashes with underscores as well.
My assumption is that the script works recursively form the parent directory on all .dwg files
I've tried using an os.rename() function but I think I need to put the (a) files into a list and
split them before possibly writing new files with the renaming convention of (b).
If anyone is wondering where this is going - I want these CAD files renamed so they can undergo
a conversion to ESRI feature class format (not shape files), and their geo-database doesn't like
feature class names beginning with numerals (thus the _ prefix), nor does it like dashes.

The following code should do. But test it before run please, I just tested the regex expression here, not the whole program.
import re
import sys, os
targetfolder = <your CAD file root folder>
for root, dirs, files in os.walk(targetfolder):
for f in files:
if os.path.splitext(f)[1] == ".dwg":
p = re.compile(r'(?P<prefix>\d+)-(?P<mid>\w+)-(?P<last>\w+).dwg')
m = p.match(f)
if m:
newf = '_' + m.group('prefix') + '_' + m.group('mid') + '_' + m.group('last') + '.dwg'
newfile = os.path.join(root, newf)
os.rename (os.path.join(root,f), newfile)

you don't need to use regular expressions; here is a working example:
import sys, os
top = "C:\Users\Philip\AppData\Local\Temp" # use your own top level directory
os.chdir(top)
for root, dirs, files in os.walk(top):
for f in files:
if f.lower().endswith(".dwg"):
old = root + "\\" + f
new = root + "\\_" + f.replace("-","_")
os.rename(old,new)

Related

Create folders with file name and rename part of it

i have some pdfs files that i need to create folders with part of your name and move the pdfs to the folders.
cwd = os.getcwd()
padrao = '_V1_A0V0_T07-54-369-664_S00001.pdf'
for file in glob.glob("*.pdf"):
dst = cwd + "\\" + file.replace(str(padrao), '').replace('P', '')
os.mkdir(dst)
shutil.move(file, dst)
ex: I have the file P9883231_V1_A0V0_T07-54-369-664_S00001.pdf, P9883231_V1_A0V0_T07-54-369-664_S00002.pdf and
P1235567_V1_A0V0_T07-54-369-664_S00001.pdf.
In this example I need the script to create two folders: 9883231 and 1234567. (the part in italics must be the name of the folder)
notice that in my code I remove the unwanted parts to create the folder, the 'P' at the beginning and part of padrao = '_V1_A0V0_T07-54-369-664_S00001.pdf'
The problem is that at the end of the padrao the number can be variable, the file can end with "02.pdf" , "03.pdf"
In the example I mentioned above, the folder 9883231 should contain both files.
Regular expressions can do the trick here:
import re
import os
import glob
import shutil
cwd = os.getcwd()
padrao = '_V1_A0V0_T07-54-369-664_S000'
for file in glob.glob("*.pdf"):
dst = os.path.join(cwd, re.findall("P(.*)" + padrao + "\d{2}.pdf", file)[0])
os.mkdir(dst)
shutil.move(file, dst)
Notice that I remove the part of padrao that varies.
The regex matches all strings that begin ith a P, followed by the padrao string value, followed by 2 digits, followed by .pdf; and takes the first occurence (no check is made wether it found anything here ...)
Also, it is better practice to use os.path.join() to avoid issues when creating path strings (when whanging os notably)

Change suffix of multiple files

I have multiple text files with names containing 6 groups of period-separated digits matching the pattern year.month.day.hour.minute.second.
I want to add a .txt suffix to these files to make them easier to open as text files.
I tried the following code and I I tried with os.rename without success:
Question
How can I add .txt to the end of these file names?
path = os.chdir('realpath')
for f in os.listdir():
file_name = os.path.splitext(f)
name = file_name +tuple(['.txt'])
print(name)
You have many problems in your script. You should read each method's documentation before using it. Here are some of your mistakes:
os.chdir('realpath') - Do you really want to go to the reapath directory?
os.listdir(): − Missing argument, you need to feed a path to listdir.
print(name) - This will print the new filename, not actually rename the file.
Here is a script that uses a regex to find files whose names are made of 6 groups of digits (corresponding to your pattern year.month.day.hour.minute.second) in the current directory, then adds the .txt suffix to those files with os.rename:
import os
import re
regex = re.compile("[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+[.][0-9]+[.][0-9]+")
for filename in os.listdir("."):
if regex.match(filename):
os.rename(filename, filename + ".txt")

How to remove characters from multiple files in python

I'm, trying to write a simple program to batch rename files in a folder.
file format:
11170_tcd001-20160824-094716.txt
11170_tcd001-20160824-094716.rst
11170_tcd001-20160824-094716.raw
I have 48 of the above with a different 14 digit character configuration after the first "-".
My final goal is to convert the above to:
11170_tcd001.txt
11170_tcd001.rst
11170_tcd001.raw
I know it's possible to os.rename files in python. However, I can't figure out how to batch rename multiple files with a different character configuration.
Is this possible?
some pseudocode below of what I would like to achieve.
import os
pathiter = (os.path.join(root, filename)
for root, _, filenames in os.walk(folder)
for filename in filenames
)
for path in pathiter:
newname = path.replace('14 digits.txt', ' 0 digits.txt')
if newname != path:
os.rename(path,newname)
If you are looking for a non-regex approach and considering your files all match that particular pattern you are expecting, what you can do first is get the extension of the file using splitext:
from os.path import splitext
file_name = '11170_tcd001-20160824-094716.txt'
extension = splitext(file_name)[1]
print(extension) # outputs: .txt
Then, with the extension in hand, split the file_name on the - and get the first item since you know that is the part that you want to keep:
new_filename = file_name.split('-')[0]
print(new_filename) # 11170_tcd001
Now, append the extension:
new_filename = new_filename + extension
print(new_filename) # 11170_tcd001.txt
Now you can proceed with the rename:
os.rename(file_name, new_filename)
You should probably try using regular expressions, like
import re
<...>
newfilename = re.sub(r'-\d{8}-\d{6}\b', '', oldfilename)
<...>
This will replace any 'hyphen, 8 digits, hyphen, 6 digits' not followed by letter, digit or underscore with empty string in your filename. Hope I got you right.

os.rename error: The System Cannot Find The Path Specified

I need to apply a new naming convention to files across a lot of subdirectories. For example, the files in one subdirectory might be:
ABC (E) String of Text.txt
ABC (E) String of Text.ocr.txt
ABC (E) String of Text.pdf
They need to all be renamed to follow this convention:
ABC String of Text (E).txt
ABC String of Text (E).ocr.txt
ABC String of Text (E).pdf
Here's what I've got so far...
import os, re
regex = re.compile('\s\([a-zA-Z]+\)')
path = os.path.expanduser('~/Google Drive/Directory/Subdirectory/')
for files in os.walk(path):
for name in files:
strname = str(name)
oldName = os.path.join(path,strname)
if(regex.search(strname)):
# identifying the token that needs shuffling
token = regex.findall(oldName)
# remove the token
removed = (regex.split(oldName)[0] + ' ' +
regex.split(oldName)[1].strip())
print removed # this is where everything goes wrong
# remove the file extension
split = removed.split('.')
# insert the token at the end of the filename
reformatted = split[0] + token[0]
# reinsert the file extension
for i in range(1,len(split)):
reformatted += '.' + split[i]
os.rename(oldName,reformatted)
It ends up trying to rename the files by pulling a substring from a list of files in the directory, but includes list-related characters like "[" and "'", resulting in WindowsError: [Error 3] The system cannot find the path specified.
Example:
C:\Users\Me/Google Drive/Directory/Subdirectory/['ABC String of Text.txt', 'ABC
My hope is that someone can see what I'm trying to accomplish and point me in the right direction.
Your problem is with os.walk which doesn't do what you want for the way you're using it: see https://docs.python.org/2/library/os.html#os.walk
Generate the file names in a directory tree by walking the tree either
top-down or bottom-up. For each directory in the tree rooted at
directory top (including top itself), it yields a 3-tuple (dirpath,
dirnames, filenames).
Perhaps you mean to do something like:
for (dirpath, dirnames, filenames) in os.walk(path):
for filename in filenames:
oldName = os.path.join(dirpath, filename)
...

Copy multiple files with control

I want to copy multiple files in one directory and copy and rename the file in increments of 500. For example the first 500 files in C:\Pics (with random original names) will be renamed 500-1000 and the new directory they are placed in is called 500…….files 1000-1500 would go into directory 1000 and so on.
The current code does not rename the files put instead puts it in a new directory with the correct number. This was just a start. I believe the code below Is a good start can anyone help me modify to get the results desired?
import os, glob
target = 'C:\Pics'
prefix = 'p0'
os.chdir(target)
allfiles = os.listdir(target)
count = 500
for filename in allfiles:
if not glob.glob('*.jpg'): continue
dirname = prefix + str(count)
target = os.path.join(dirname, filename)
os.renames(filename, target)
count +=1
os.listdir and glob.glob are similar functions. They both return lists of files/dirs, so they don't belong in the same loop (at least not the way you're trying to use them). The main difference is that os.listdir just takes a directory and returns basically *.* from it (minus . and ..), where as glob.glob expects a "globbing pattern" which can contain * ? [] in a restricted regex format. The function you might be thinking of here (instead of glob.glob) is fnmatch.fnmatch, which applies a globbing pattern to a single file name.
os.listdir(path)
Return a list containing the names of the entries in the directory
given by path. The list is in arbitrary order. It does not include the
special entries '.' and '..' even if they are present in the
directory.
Availability: Unix, Windows.
Changed in version 2.3: On Windows NT/2k/XP and Unix, if path is a Unicode object, the result > will be a list of Unicode objects. Undecodable filenames will still be returned as string
objects.
glob.glob(pathname)
Return a possibly-empty list of path names that
match pathname, which must be a string containing a path
specification. pathname can be either absolute (like
/usr/src/Python-1.5/Makefile) or relative (like ../../Tools//.gif),
and can contain shell-style wildcards. Broken symlinks are included in
the results (as in the shell).
Sorry, too lazy to actually mock up files and test this, but then I'd be doing all the work for you. But this should work (or be a darn close to what I think you're aiming at). ;)
import os
import fnmatch
import os.path
target = 'C:\Pics'
os.chdir(target)
allfiles = os.listdir(target)
count = 500
for filename in allfiles:
if not fnmatch.fnmatch(filename, '*.jpg'):
continue
if count % 500 == 0:
dirname = 'p%04d' % count
if not os.path.exists(dirname):
os.mkdir(dirname)
target = os.path.join(dirname, '%d.jpg' % count)
os.rename(filename, target)
count += 1

Categories

Resources