renaming a file based on it's content, then moving it to a new directory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thom21
    New Member
    • Feb 2010
    • 1

    renaming a file based on it's content, then moving it to a new directory

    hi,

    I'm having trouble figuring out exactly how to solve the following problem:

    I have an xml document "doc1.xml" that needs to be renamed and then moved to a new folder directory ("dir0/dir1/dir2/newname.xml").

    The details for renaming the file and the folder creation are all contained in various xml nodes in the file itself. Can someone help me understand how I can open a file, use it's contents to create a new folder, then rename the file i've opened and place it in this new directory.

    I've been able to create the new directories just fine, but I can't seem to figure out how to move the file. I'm a bit new to python but I'm almost certain this is a task that can easily be accomplished.

    Thanks
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    You will need to use the os module's function rename:

    Code:
    import os
    os.rename('/home/motoma/testfile', '/var/www/motomastyle.com/index.html')

    Comment

    Working...