jython - moving files with unconventional file extensions in Python -
i stuck @ part of script supposed perform following: a. iterate through source directory. b. move each file (name = guid.file extension) destination folder named file's guid.
in theory, problem simple enough solve in python os.walk() , os.rename(). complication file extension of these files unconventional shown screenshot:
as workaround, using commons.io java libraries. yet script erroring on last 3 lines when trying instantiate file objects. doing wrong?
script:
import os import codecs import shutil import datetime import sys org.apache.commons.io import fileutils org.apache.commons.io.filefilter import truefilefilter java.io import file sourcedirectoryroot = 'p:/output/export18/bad' sourcedirectory = sourcedirectoryroot + '/natives' 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 error: file "<script>", line 21 file s = new file(path) ^ syntaxerror: no viable alternative @ input 's' @ org.python.core.parserfacade.fixparseerror(parserfacade.java:92)
you're getting parser error due invalid syntax.
in python don't specify variable types, or use new
instantiation new objects
file s = new file(path) file d = new file(destdirectory)
should be
s = file(path) d = file(destdirectory)
from jython docs:
if have java class
public class beach { private string name; private string city; public beach(string name, string city){ this.name = name; this.city = city; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getcity() { return city; } public void setcity(string city) { this.city = city; } }
you use jython so:
>>> import beach >>> beach = beach("cocoa beach","cocoa beach") >>> beach.getname() u'cocoa beach' >>> print beach.getname() cocoa beach
Comments
Post a Comment