I am stuck at a part of my script that is supposed to perform the following: a. Iterate through a source directory. b. Move each file (name = GUID.file extension) to a destination folder that is named as that file's guid.
In theory, this problem is simple enough to solve in Python with the os.walk() and os.rename(). The complication is that the file extension for some of these files are unconventional as shown by this screenshot:
As a workaround, I using Commons.IO Java libraries. Yet my script is erroring on the last 3 lines when I am trying to instantiate File objects. What am I doing wrong?
Script:
import os
import codecs
import shutil
import datetime
import sys
from org.apache.commons.io import FileUtils
from org.apache.commons.io.filefilter import TrueFileFilter
from java.io import File
sourceDirectoryRoot = 'P:/Output/Export18/BAD'
sourceDirectory = sourceDirectoryRoot + '/NATIVES'
for source in FileUtils.iterateFiles(File(sourceDirectory),TrueFileFilter.INSTANCE,TrueFileFilter.INSTANCE):
path = source.getPath().replace('\\', '/')
file = source.getName()
fileparts = path.split(".")
ext = fileparts[len(fileparts) - 1]
destDirectory = sourceDirectoryRoot + '/{' + file[0:36] + '}/' + '[Document Renamed].' + ext
print path
print destDirectory
File s = new File(path)
File d = new File(destDirectory)
FileUtils.moveFile(s, d)
Error (partial string):
Script failed due to an error:
File "<script>", line 21
File s = new File(path)
^
SyntaxError: no viable alternative at input 's'
at org.python.core.ParserFacade.fixParseError(ParserFacade.java:92)