Python java.io.File() Examples

The following are 30 code examples of java.io.File(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module java.io , or try the search function .
Example #1
Source File: lucene_search.py    From dl4ir-webnav with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def create_index():

    lucene.initVM()
    if os.path.exists(prm.index_folder):
        shutil.rmtree(prm.index_folder)

    indexDir = SimpleFSDirectory(File(prm.index_folder))
    writerConfig = IndexWriterConfig(Version.LUCENE_4_10_1, StandardAnalyzer())
    writer = IndexWriter(indexDir, writerConfig)
    wk = wiki.Wiki(prm.pages_path)

    print "%d docs in index" % writer.numDocs()
    print "Reading files from wikipedia..."
    n = 0
    for l in wk.get_text_iter():
        doc = Document()
        doc.add(Field("text", l, Field.Store.YES, Field.Index.ANALYZED))
        doc.add(Field("id", str(n), Field.Store.YES, Field.Index.ANALYZED))
        writer.addDocument(doc)
        n += 1
        if n % 100000 == 0:
            print 'indexing article', n
    print "Indexed %d docs from wikipedia (%d docs in index)" % (n, writer.numDocs())
    print "Closing index of %d docs..." % writer.numDocs()
    writer.close() 
Example #2
Source File: javashell.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def execute( self, cmd ):
        """Execute cmd in a shell, and return the java.lang.Process instance.
        Accepts either a string command to be executed in a shell,
        or a sequence of [executable, args...].
        """
        shellCmd = self._formatCmd( cmd )

        env = self._formatEnvironment( self.environment )
        try:
            p = Runtime.getRuntime().exec( shellCmd, env, File(os.getcwdu()) )
            return p
        except IOException, ex:
            raise OSError(
                0,
                "Failed to execute command (%s): %s" % ( shellCmd, ex )
                )

    ########## utility methods 
Example #3
Source File: filetree.py    From inql with Apache License 2.0 6 votes vote down vote up
def __init__(self, dir=None, label=None):
        if not dir: dir = os.getcwd()
        if not label: label = "FileTree"
        dir = File(dir)
        self._dir = dir
        self.this = JPanel()
        self.this.setLayout(BorderLayout())

        # Add a label
        self.this.add(BorderLayout.PAGE_START, JLabel(label))

        # Make a tree list with all the nodes, and make it a JTree
        tree = JTree(self._add_nodes(None, dir))
        tree.setRootVisible(False)
        self._tree = tree

        # Lastly, put the JTree into a JScrollPane.
        scrollpane = JScrollPane()
        scrollpane.getViewport().add(tree)
        self.this.add(BorderLayout.CENTER, scrollpane) 
Example #4
Source File: javapath.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def getatime(path):
    # We can't detect access time so we return modification time. This
    # matches the behaviour in os.stat().
    path = _tostr(path, "getatime")
    f = File(sys.getPath(path))
    if not f.exists():
        raise OSError(0, 'No such file or directory', path)
    return f.lastModified() / 1000.0


# expandvars is stolen from CPython-2.1.1's Lib/ntpath.py:

# Expand paths containing shell variable substitutions.
# The following rules apply:
#       - no expansion within single quotes
#       - no escape character, except for '$$' which is translated into '$'
#       - ${varname} is accepted.
#       - varnames can be made out of letters, digits and the character '_'
# XXX With COMMAND.COM you can use any characters in a variable name,
# XXX except '^|<>='. 
Example #5
Source File: javashell.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def execute( self, cmd ):
        """Execute cmd in a shell, and return the java.lang.Process instance.
        Accepts either a string command to be executed in a shell,
        or a sequence of [executable, args...].
        """
        shellCmd = self._formatCmd( cmd )

        env = self._formatEnvironment( self.environment )
        try:
            p = Runtime.getRuntime().exec( shellCmd, env, File(os.getcwdu()) )
            return p
        except IOException, ex:
            raise OSError(
                0,
                "Failed to execute command (%s): %s" % ( shellCmd, ex )
                )

    ########## utility methods 
Example #6
Source File: javapath.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def getatime(path):
    # We can't detect access time so we return modification time. This
    # matches the behaviour in os.stat().
    path = _tostr(path, "getatime")
    f = File(sys.getPath(path))
    if not f.exists():
        raise OSError(0, 'No such file or directory', path)
    return f.lastModified() / 1000.0


# expandvars is stolen from CPython-2.1.1's Lib/ntpath.py:

# Expand paths containing shell variable substitutions.
# The following rules apply:
#       - no expansion within single quotes
#       - no escape character, except for '$$' which is translated into '$'
#       - ${varname} is accepted.
#       - varnames can be made out of letters, digits and the character '_'
# XXX With COMMAND.COM you can use any characters in a variable name,
# XXX except '^|<>='. 
Example #7
Source File: javashell.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def execute( self, cmd ):
        """Execute cmd in a shell, and return the java.lang.Process instance.
        Accepts either a string command to be executed in a shell,
        or a sequence of [executable, args...].
        """
        shellCmd = self._formatCmd( cmd )

        env = self._formatEnvironment( self.environment )
        try:
            p = Runtime.getRuntime().exec( shellCmd, env, File(os.getcwdu()) )
            return p
        except IOException, ex:
            raise OSError(
                0,
                "Failed to execute command (%s): %s" % ( shellCmd, ex )
                )

    ########## utility methods 
Example #8
Source File: test_support.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def make_jar_classloader(jar):
        import os
        from java.net import URL, URLClassLoader
        from java.io import File

        if isinstance(jar, bytes): # Java will expect a unicode file name
            jar = jar.decode(sys.getfilesystemencoding())
        jar_url = File(jar).toURI().toURL().toString()
        url = URL(u'jar:%s!/' % jar_url)

        if is_jython_nt:
            # URLJarFiles keep a cached open file handle to the jar even
            # after this ClassLoader is GC'ed, disallowing Windows tests
            # from removing the jar file from disk when finished with it
            conn = url.openConnection()
            if conn.getDefaultUseCaches():
                # XXX: Globally turn off jar caching: this stupid
                # instance method actually toggles a static flag. Need a
                # better fix
                conn.setDefaultUseCaches(False)

        return URLClassLoader([url])

# Filename used for testing 
Example #9
Source File: javapath.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def getatime(path):
    # We can't detect access time so we return modification time. This
    # matches the behaviour in os.stat().
    path = _tostr(path, "getatime")
    f = File(sys.getPath(path))
    if not f.exists():
        raise OSError(0, 'No such file or directory', path)
    return f.lastModified() / 1000.0


# expandvars is stolen from CPython-2.1.1's Lib/ntpath.py:

# Expand paths containing shell variable substitutions.
# The following rules apply:
#       - no expansion within single quotes
#       - no escape character, except for '$$' which is translated into '$'
#       - ${varname} is accepted.
#       - varnames can be made out of letters, digits and the character '_'
# XXX With COMMAND.COM you can use any characters in a variable name,
# XXX except '^|<>='. 
Example #10
Source File: lucene_tools.py    From TAGME-Reproducibility with MIT License 6 votes vote down vote up
def __init__(self, index_dir, use_ram=False, jvm_ram=None):
        global lucene_vm_init
        if not lucene_vm_init:
            if jvm_ram:
                # e.g. jvm_ram = "8g"
                print "Increased JVM ram"
                lucene.initVM(vmargs=['-Djava.awt.headless=true'], maxheap=jvm_ram)
            else:
                lucene.initVM(vmargs=['-Djava.awt.headless=true'])
            lucene_vm_init = True
        self.dir = SimpleFSDirectory(File(index_dir))

        self.use_ram = use_ram
        if use_ram:
            print "Using ram directory..."
            self.ram_dir = RAMDirectory(self.dir, IOContext.DEFAULT)
        self.analyzer = None
        self.reader = None
        self.searcher = None
        self.writer = None
        self.ldf = None
        print "Connected to index " + index_dir 
Example #11
Source File: engine_withlucene.py    From txtorg with MIT License 6 votes vote down vote up
def _init_index(self):

        if not os.path.exists(self.corpus.path):
            os.mkdir(self.corpus.path)
        try:
            searcher = IndexSearcher(SimpleFSDirectory(File(self.corpus.path)), True)
        #except lucene.JavaError:
        except:
            analyzer = self.corpus.analyzer
            writer = IndexWriter(SimpleFSDirectory(File(self.corpus.path)), analyzer, True, IndexWriter.MaxFieldLength.LIMITED)
            writer.setMaxFieldLength(1048576)
            writer.optimize()
            writer.close()

        self.lucene_index = SimpleFSDirectory(File(self.corpus.path))
        self.searcher = IndexSearcher(self.lucene_index, True)
        self.reader = IndexReader.open(self.lucene_index, True)
        self.analyzer = self.corpus.analyzer 
Example #12
Source File: engine_withlucene.py    From txtorg with MIT License 6 votes vote down vote up
def import_csv(self, csv_file):

        try:
            writer = IndexWriter(SimpleFSDirectory(File(self.corpus.path)), self.analyzer, False,
                                        IndexWriter.MaxFieldLength.LIMITED)
            changed_rows = addmetadata.add_metadata_from_csv(self.searcher, self.reader, writer, csv_file,self.args_dir,
                                                             new_files=True)
            writer.close()
        except UnicodeDecodeError:
            try:
                writer.close()
            except:
                pass
            self.parent.write({'error': 'CSV import failed: file contained non-unicode characters. Please save the file with UTF-8 encoding and try again!'})
            return
        self.parent.write({'message': "CSV import complete: %s rows added." % (changed_rows,)}) 
Example #13
Source File: javashell.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def execute( self, cmd ):
        """Execute cmd in a shell, and return the java.lang.Process instance.
        Accepts either a string command to be executed in a shell,
        or a sequence of [executable, args...].
        """
        shellCmd = self._formatCmd( cmd )

        env = self._formatEnvironment( self.environment )
        try:
            p = Runtime.getRuntime().exec( shellCmd, env, File(os.getcwdu()) )
            return p
        except IOException, ex:
            raise OSError(
                0,
                "Failed to execute command (%s): %s" % ( shellCmd, ex )
                )

    ########## utility methods 
Example #14
Source File: javashell.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def execute( self, cmd ):
        """Execute cmd in a shell, and return the java.lang.Process instance.
        Accepts either a string command to be executed in a shell,
        or a sequence of [executable, args...].
        """
        shellCmd = self._formatCmd( cmd )

        env = self._formatEnvironment( self.environment )
        try:
            p = Runtime.getRuntime().exec( shellCmd, env, File(os.getcwd()) )
            return p
        except IOException, ex:
            raise OSError(
                0,
                "Failed to execute command (%s): %s" % ( shellCmd, ex )
                )

    ########## utility methods 
Example #15
Source File: javapath.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def isdir(path):
    """Test whether a path is a directory"""
    path = _tostr(path, "isdir")
    return File(sys.getPath(path)).isDirectory() 
Example #16
Source File: javapath.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def getsize(path):
    path = _tostr(path, "getsize")
    f = File(sys.getPath(path))
    size = f.length()
    # Sadly, if the returned length is zero, we don't really know if the file
    # is zero sized or does not exist.
    if size == 0 and not f.exists():
        raise OSError(0, 'No such file or directory', path)
    return size 
Example #17
Source File: javapath.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def join(path, *args):
    """Join two or more pathname components, inserting os.sep as needed"""
    path = _tostr(path, "join")
    f = File(path)
    for a in args:
        a = _tostr(a, "join")
        g = File(a)
        if g.isAbsolute() or len(f.getPath()) == 0:
            f = g
        else:
            if a == "":
                a = os.sep
            f = File(f, a)
    return asPyString(f.getPath()) 
Example #18
Source File: javapath.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def isfile(path):
    """Test whether a path is a regular file"""
    path = _tostr(path, "isfile")
    return File(sys.getPath(path)).isFile() 
Example #19
Source File: javapath.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def isdir(path):
    """Test whether a path is a directory"""
    path = _tostr(path, "isdir")
    return File(sys.getPath(path)).isDirectory() 
Example #20
Source File: javapath.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def isabs(path):
    """Test whether a path is absolute"""
    path = _tostr(path, "isabs")
    return File(path).isAbsolute() 
Example #21
Source File: javapath.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def getmtime(path):
    path = _tostr(path, "getmtime")
    f = File(sys.getPath(path))
    if not f.exists():
        raise OSError(0, 'No such file or directory', path)
    return f.lastModified() / 1000.0 
Example #22
Source File: javapath.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def exists(path):
    """Test whether a path exists.

    Returns false for broken symbolic links.

    """
    path = _tostr(path, "exists")
    return File(sys.getPath(path)).exists() 
Example #23
Source File: javapath.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def basename(path):
    """Return the final component of a pathname"""
    path = _tostr(path, "basename")
    return asPyString(File(path).getName()) 
Example #24
Source File: javapath.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def dirname(path):
    """Return the directory component of a pathname"""
    path = _tostr(path, "dirname")
    result = asPyString(File(path).getParent())
    if not result:
        if isabs(path):
            result = path # Must be root
        else:
            result = ""
    return result 
Example #25
Source File: javapath.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def _abspath(path):
    # Must use normpath separately because getAbsolutePath doesn't normalize
    # and getCanonicalPath would eliminate symlinks.
    return normpath(asPyString(File(sys.getPath(path)).getAbsolutePath())) 
Example #26
Source File: index.py    From hoaxy-backend with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, index_dir, mode, date_format='%Y-%m-%dT%H:%M:%S'):
        """Constructor of Indexer.

        Parameters
        ----------
        index_dir : string
            The location of lucene index
        mode : string
            The mode when opening lucene index. Available values are:
                'create', open new index and overwriting over index,
                'append', open existed index and append.
                'create_or_append', if `index_dir` exists, 'append',
                else 'create'
        date_format : string
            We save datetime field as string, `date_format` specify how to
            format datetime into string.
        """
        # self.store = FSDirectory.open(File(index_dir))
        self.store = FSDirectory.open(Paths.get(index_dir))
        # self.analyzer = StandardAnalyzer(Version.LUCENE_CURRENT)
        self.analyzer = StandardAnalyzer()
        # self.config = IndexWriterConfig(Version.LUCENE_CURRENT, self.analyzer)
        self.config = IndexWriterConfig(self.analyzer)
        self.mode = mode
        self.date_format = date_format
        if mode == 'create_or_append':
            self.config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND)
        elif mode == 'create':
            self.config.setOpenMode(IndexWriterConfig.OpenMode.CREATE)
        elif mode == 'append':
            self.config.setOpenMode(IndexWriterConfig.OpenMode.APPEND)
        else:
            raise ValueError('Invalid mode %s', mode)
        self.writer = IndexWriter(self.store, self.config) 
Example #27
Source File: javapath.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def join(path, *args):
    """Join two or more pathname components, inserting os.sep as needed"""
    path = _tostr(path, "join")
    f = File(path)
    for a in args:
        a = _tostr(a, "join")
        g = File(a)
        if g.isAbsolute() or len(f.getPath()) == 0:
            f = g
        else:
            if a == "":
                a = os.sep
            f = File(f, a)
    return asPyString(f.getPath()) 
Example #28
Source File: javapath.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def expanduser(path):
    if path[:1] == "~":
        c = path[1:2]
        if not c:
            return gethome()
        if c == os.sep:
            return asPyString(File(gethome(), path[2:]).getPath())
    return path 
Example #29
Source File: javapath.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def _realpath(path):
    try:
        return asPyString(File(sys.getPath(path)).getCanonicalPath())
    except java.io.IOException:
        return _abspath(path) 
Example #30
Source File: javapath.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def getmtime(path):
    path = _tostr(path, "getmtime")
    f = File(sys.getPath(path))
    if not f.exists():
        raise OSError(0, 'No such file or directory', path)
    return f.lastModified() / 1000.0