You can use the Transform node to rename files using the following Python code. The generic way to handle renaming a file in Python is:
import os
os.rename(r'file path\OLD file name.filetype',r'file path\NEW file name.filetype')
EXAMPLES:
For Windows:
import os
os.rename(r'C:\Users\Bob\Desktop\Test\test_file.txt', r'C:\Users\Bob\Desktop\Test\new_test_file.txt')
For Linux:
import os
os.rename(r'/home/Bob/Test/test_file.txt', r'/home/Bob/Test/new_test_file.txt')
The "r" in front of the file name is not really necessary with Linux since it uses forward slashes for the path separators rather than the backslash with Windows. However, it is best practice to include it to make the code more portable.
You could also try the following as it may work on both Linux and Windows:
import shutil
shutil.move(old,new)
FULL CODE FOR THE TRANSFORM NODE:
#Configure Fields:
out1 += in1
out1.OldFile=str
out1.NewFile=str
out1.Status=str
import shutil
import os
#Process records:
out1 += in1
OldFile=in1.FileName
#supply your own expression here to build the new filename
NewFile=os.path.join(os.path.dirname(OldFile),"QQQ.xlsx")
out1.Status="Success"
try:
shutil.move(OldFile,NewFile)
except:
out1.Status="Failure"
out1.OldFile=OldFile
out1.NewFile=NewFile
Comments
0 comments
Please sign in to leave a comment.