Renaming Files

I’ve created a script that writes to a log file, and would like to rename it whenever it reaches a certain size. But, renameTo() always returns zero. Ideas? Btw, in the code section where I append to the log file, it is explicily closed after every write.

import java
import os
from java.io import File

url = java.net.URL("http://localhost:8088/MessageStore/LogFile.txt")
checkFile=File(url.getFile())
print checkFile.length()
if checkFile.length()>100000:
	newUrl = java.net.URL("http://localhost:8088/MessageStore/LogFile.BU")
	newFile = File(newUrl.getFile())
	print newFile
	print checkFile.renameTo(newFile)

I have to admit a little ignorance here, but a quick search brings up lots of information about renameTo() being very finicky. It’s probably failing because something else is touching that file still. Many anti-virus programs scan files after they are modified, have you tried adding a 5 second pause before renaming it? How about using a copy and delete instead of the renameTo?

This isn’t working because your files aren’t, well, files. They’re HTTP addresses. Java will let you pretend an HTTP URL is a “File” to a limited extent (you can read from it) but thats it - no writing or renaming or deleting or any of that kind of action.

The same thing happens with and without AV running, and I can manually rename the file with no problem even when I'm writing to the file every 50ms. I've also tried using python's os.rename() and other implementations of java, and get the same results. Like you say, renaming files on a windows box programatically seems pretty finicky. Who would have thunk it.

Anyway, I'm doing it a different way now. I just concatenate the standard log file name with the year and julian day, and it just creates a new unique file each day. I'll add logic later to handle the case where the files grow too large within a single day, but this gets me by. My original intent was to create log files similar to the way your wrapper logs are created (wrapper.log, wrapper.log.1, etc), but this works just as well.

Ok, I sat down last night and figured this out, and wanted to post it in case someone else wanted to build a simple logger.

Anyway, here are some code examples that can be added to buttons that will append to, rename, and delete files using both python script and the java libraries.


# Write to file with java library (the '1' in the constructor sets it to append mode, otherwise leave it out)
import java.io as F
line='This is a test \n'*1000
fstream = F.FileWriter("C:\\MessageStore\\DeleteMe.txt",1)
out = F.BufferedWriter(fstream)
out.write(line)
out.close()

#####################################################
# Write to file with python (the 'a' sets it to append mode, otherwise use 'w')
import os
line='This is a test \n'*1000
f=open("C:\\MessageStore\\DeleteMe.txt",'a')
f.write(line)
f.close()

#####################################################
# Rename file with Java library
try:
	import java.io as F
	f=F.File("C:\\MessageStore\\DeleteMe.txt")
	newf=F.File("C:\\MessageStore\\KeepMe.txt")
	f.renameTo(newf)
except:
	print 'Failed'


#####################################################
# Rename file with Python
try:
	import os
	f="C:\\MessageStore\\DeleteMe.txt"	
	newf="C:\\MessageStore\\KeepMe.txt"
	os.rename(f,newf)
except:
	print 'Failed'

#####################################################
# Delete file with java library. The items in the print statement
# could be used to log why the delete failed
try:
	import java.io as F
	f=F.File("C:\\MessageStore\\DeleteMe.txt")
	print f, f.exists(), f.canWrite(), f.isDirectory(), f.canRead()
	f.delete()
except:
	print 'Nothing deleted'


#####################################################
# Delete file with Python
try:
	import os
	f="C:\\MessageStore\\DeleteMe.txt"
	os.remove(f)
except:
	print 'Nothing deleted'