January 29th, 2013, 12:15 PM
Need help renaming files based on their file path then moving file to a new folder
So I am trying to rename files based on the treepath it is in, then move the renamed files to a specific folder (based on its name).
So, for example, I have a file in path L:\a\b\c\d\e\f\file.pdf
I want to rename "file.pdf" to "d e f"
Also, all the subfolders branch off at c so I want python to scan all the documents in the subfolders contained in folder c to be renamed according to the aforementioned pattern. I.e., L:\a\b\c\x\y\z\file.pdf, file.pdf renamed to "x y z"; L:\a\b\c\q\r\s\file.pdf, file.pdf renamed to "q r s"; etc.
Then, I want to move all those files to a new, already existing folder, based on their names. So say for example for file "d e f" I would want to move to L:a\b\1\d\f\e.
I am quite new at coding at Python and I have a few pieces of the puzzle kind of worked out but I am having a lot of trouble. Here is some of my code but I don't think it will prove very useful.
For this code, I have to drop the file into CMD with the .py file. It spits out the name I want (but with extra spaces that I don't want), it doesn't actually rename the file, and is done only with the specific file I dropped into CMD when I would rather have the code look through all of the subfolders and do it automatically. Please note that my code (specifically, lines 6-7) is specific to how the folder I want is actually named, I obfuscated the name of the tree path for confidentiality reasons and it just makes it easier to understand.
from sys import argv
script, filename = argv
txt = open(filename)
print "Here's your file %r:" % filename
string = "%r" % filename
print string [94:-17]
line = string [94:-17]
line = "%r" % line
for char in '\\':
line = line.replace (char, ' ')
Doing some homework, this code will search and rename all the files in the directory I want, however it does not name it the way that I want. Again, this isn't really helpful but it is what I have.
import glob, os
def rename(dir, pattern, titlePattern):
for pathAndFilename in glob.iglob(os.path.join(dir, pattern)):
title, ext = os.path.splitext(os.path.basename(pathAndFilename))
os.path.join(dir, titlePattern % title + ext))
rename(r'L:\a\b\c\', r'*.pdf', r'new(%s)'
And then for actually moving the files, I don't have any code made yet - I am pretty lost. I understand that this is a lot of work, but I would greatly appreciate it if someone could help me out.
January 29th, 2013, 07:54 PM
DOS has a move command. You should be able to put the files in a new directory and change the name at the same time. Drop, spit, extra space. Sorry, your post makes my head sore.
Command line to run doctest:
This file is named q.py
$ python -m doctest q.py
want 'L:\a\b\c\d e f'
do you really mean to lose the file extension information?
def change(s,brk,sep=os.path.sep,replacement=' '):
>>> r'L:\a\b\c\d e f' == change(r'L:\a\b\c\d\e\f\file.pdf','c','\\')
fields = s.split(sep)
assert brk in fields
i = fields.index(brk)+1
[/code] are essential for python code and Makefiles!