user2377057
user2377057

Reputation: 183

Rename a file in Python

I’m a self-taught python newbie of a couple of days. I’ve got a basic understanding of the way python operates, but I’m really stuck with the following.

I have a list of text files which are exchange server mail dumps of mailbox name. I’ve hundreds of these text files and they are currently in the name format Priv_date.txt eg Priv_02JAN2004.txt. I need to be able to tell what server they’ve come from so within these text files I want to read the 10 line which has actual mail server name (Server: MAILSERVER1) and add or append this to the original filename.

What I’d like to end up with are filenames that read MAILSERVER1_PRIV_02JAN2004.txt. I’m getting myself confused over what I can and can’t do to the filepath and name but can’t see what I’m doing wrong. I’ve got as far as this:

import os,sys

folder = "c://EDB_TEMP"

for root, dirs, filenames in os.walk(folder):
    for filename in filenames:
        fullpath=os.path.join(root,filename)
        filename_split = os.path.splitext(fullpath)

        #print fullpath
        #print filename

        with open (fullpath, "r") as tempfile:
            for line in tempfile.readlines():
                if "Server:" in line:
                    os.rename(tempfile,+line[10:]+fullpath)

But I keep getting this error:

error is TypeError: bad operand type for unary +: 'str'

Upvotes: 2

Views: 2008

Answers (2)

rcbevans
rcbevans

Reputation: 8872

This code works and does what you describe

#Also include Regular Expression module, re
import os,sys,re

#Set root to the folder you want to check
folder = "%PATH_TO_YOUR_FOLDER%"

#Walk through the folder checking all files
for root, dirs, filenames in os.walk(folder):
    #For each file in the folder
    for filename in filenames:
        #Create blank strink for servername
        servername = ''
        #Get the full path to the file
        fullpath=os.path.join(root,filename)
        #Open the file as read only in tempfile
        with open (fullpath, "r") as tempfile:
            #Iterate through the lines in the file
            for line in tempfile.readlines():
                #Check if this line contains "Server: XXXXX"
                serverline= re.findall("Server: [a-zA-Z0-9]+", line)
                #If the line was found
                if serverline:
                    #Split the line around ": " and take second part as server name
                    sname = serverline[0].split(": ")
                    #Set servername variable so isn't lost outside scope of with block
                    servername = sname[1]
        #If a servername was found for that text file
        if len(servername) > 0:
            #Rename the file
            os.rename(fullpath,root+'\\'+servername+filename)

What this does is walk the directory, as you had before, finding each path. For each file, it will get the path to it, open the file and look for a line containing Server: SERVERNAME. It will then extract SERVERNAME and put it into the servername variable. When the file is finished, it will be closed and the script checks if that file produced a servername string. If it did, it renames the file by prefixing with SERVERNAME.

I had some time so decided to test it too so should do what you want

Upvotes: 2

Ayman
Ayman

Reputation: 11585

You have an error in your os.rename(tempfile,+line[10:]+fullpath) the comma seem misplaced.

The error basically says the + just after the comma cannot precede a string, which is line[10:].

Upvotes: 3

Related Questions