473,468 Members | 1,358 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

File/Directory hacks - is there a better way to do this?

I had a software package screw up a directory tree by placing all the
files in a directory of the same name. So I wound up with:

Root
file1.txt (dir)
file1.txt (file)

file2.txt (dir)
file2.txt (file)

Annoying. So I wrote the following script, which fixed the problem by
renaming and copying the file up to the root directory, removing the
intermediate directory, and finally restoring the orginal file name.

Is there a better (faster, or fewer lines of code, or more
'python-esque') way to do? In particular, I know the latest versions
of python includes iterators, generators, and decorators...but I
haven't had a chance to really read about them, or understand how to
use them properly. So I'd love to hear if the new tools could've
solved this simpler/faster.

Thanks!

------------------------------------------------------

#!/user/bin/env python

import os, os.path, shutil

top = 'c:\$user'

class fixdirectories:
def __init__(self, top):
self.top = top

def walk(self):
for root, dirs, files in os.walk(self.top, topdown=False):
rootdir, roottail = os.path.split(root)
for name in files:
if name.lower() == roottail:

oldname = os.path.join(root, name)
tmpname = os.path.join(rootdir, "PRE-" + name)
newname = os.path.join(rootdir, name)

shutil.move(oldname, tmpname)
try:
os.rmdir(root)
print "removing: " + rootdir
shutil.move(tmpname, newname)
except:
print "Could not remove: " + root

if __name__ == "__main__":
f = fixdirectories(top)
f.walk()

Jul 18 '05 #1
0 979

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: R. Rajesh Jeba Anbiah | last post by:
Q: How should I handle file upload? A: File uploading requires HTML form of content type "multipart/form-data". The file content has to be POSTed/submitted via the form and once the file is...
9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
3
by: Nate | last post by:
Hello, Maybe no response on my last post because it was too lengthy or the moderators were too busy? Could someone please post a sample *.aspx Web file (VB or C#) that calls a proxy class to the...
1
by: Geoff Berrow | last post by:
I've written a script which takes a couple of user image files and uses them to create a watermarked image. http://www.ckdog.co.uk/watermark.php I'm checking the images like so: ...
1
by: Alex VanderWoude | last post by:
I am trying to <include> some text into an XML documentation topic, but that text is stored in a file that is in a different directory than the "current" XML file. Using a relative path does not...
3
by: Mark Olbert | last post by:
I have some img tags on a master page which reference files in a top-level directory. They look like this: <img src="assets/test.gif" id="gnr" /> assets is a subdirectory of the website root. ...
10
by: Ben Finney | last post by:
Howdy all, Question: I have Python modules named without '.py' as the extension, and I'd like to be able to import them. How can I do that? Background: On Unix, I write programs intended to...
1
by: icogs | last post by:
I am fairly new to Javascript, coming from a Java & C/C++ background so the possibility of a fundamental misunderstanding on my part cannot be ruled out :-( Lets say a friend sends me some...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.