0

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:

enter image description here

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)
smac89
  • 39,374
  • 15
  • 132
  • 179
stan1220
  • 25
  • 7

1 Answers1

1

You're getting a parser error due to invalid syntax.

In Python you don't specify variable types, or use new for instantiation new objects

File s = new File(path)
File d = new File(destDirectory)

Should be

s = File(path)
d = File(destDirectory)

From the Jython docs:

If you have a 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 this from Jython like so:

>>> import Beach
>>> beach = Beach("Cocoa Beach","Cocoa Beach")
>>> beach.getName()
u'Cocoa Beach'
>>> print beach.getName()
Cocoa Beach
Peter Gibson
  • 19,086
  • 7
  • 60
  • 64