本文整理匯總了Python中java.io.File方法的典型用法代碼示例。如果您正苦於以下問題:Python io.File方法的具體用法?Python io.File怎麽用?Python io.File使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.io
的用法示例。
在下文中一共展示了io.File方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: create_index
# 需要導入模塊: from java import io [as 別名]
# 或者: from java.io import File [as 別名]
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()
示例2: _init_index
# 需要導入模塊: from java import io [as 別名]
# 或者: from java.io import File [as 別名]
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
示例3: import_csv
# 需要導入模塊: from java import io [as 別名]
# 或者: from java.io import File [as 別名]
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,)})
示例4: __init__
# 需要導入模塊: from java import io [as 別名]
# 或者: from java.io import File [as 別名]
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
示例5: execute
# 需要導入模塊: from java import io [as 別名]
# 或者: from java.io import File [as 別名]
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
示例6: getatime
# 需要導入模塊: from java import io [as 別名]
# 或者: from java.io import File [as 別名]
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 '^|<>='.
示例7: __init__
# 需要導入模塊: from java import io [as 別名]
# 或者: from java.io import File [as 別名]
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)
示例8: execute
# 需要導入模塊: from java import io [as 別名]
# 或者: from java.io import File [as 別名]
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
示例9: make_jar_classloader
# 需要導入模塊: from java import io [as 別名]
# 或者: from java.io import File [as 別名]
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
示例10: get_candidates
# 需要導入模塊: from java import io [as 別名]
# 或者: from java.io import File [as 別名]
def get_candidates(qatp):
if prm.create_index:
create_index()
lucene.initVM()
analyzer = StandardAnalyzer(Version.LUCENE_4_10_1)
reader = IndexReader.open(SimpleFSDirectory(File(prm.index_folder)))
searcher = IndexSearcher(reader)
candidates = []
n = 0
for q,a,t,p in qatp:
if n % 100 == 0:
print 'finding candidates sample', n
n+=1
q = q.replace('AND','\\AND').replace('OR','\\OR').replace('NOT','\\NOT')
query = QueryParser(Version.LUCENE_4_10_1, "text", analyzer).parse(QueryParser.escape(q))
hits = searcher.search(query, prm.max_candidates)
c = []
for hit in hits.scoreDocs:
doc = searcher.doc(hit.doc)
c.append(doc.get("id"))
candidates.append(c)
return candidates
示例11: import_csv_with_content
# 需要導入模塊: from java import io [as 別名]
# 或者: from java.io import File [as 別名]
def import_csv_with_content(self, csv_file, content_field):
try:
writer = IndexWriter(SimpleFSDirectory(File(self.corpus.path)), self.analyzer, False, IndexWriter.MaxFieldLength.LIMITED)
changed_rows = addmetadata.add_metadata_and_content_from_csv(self.searcher, self.reader, writer, csv_file, content_field, self.args_dir)
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,)})
示例12: reindex
# 需要導入模塊: from java import io [as 別名]
# 或者: from java.io import File [as 別名]
def reindex(self):
writer = IndexWriter(SimpleFSDirectory(File(self.corpus.path)), self.corpus.analyzer, False, IndexWriter.MaxFieldLength.LIMITED)
indexutils.reindex_all(self.reader, writer, self.corpus.analyzer)
writer.optimize()
writer.close()
self.parent.write({'message': "Reindex successful. Corpus analyzer is now set to %s." % (self.corpus.analyzer_str,)})
self.parent.write({'status': "Ready!"})
示例13: dirname
# 需要導入模塊: from java import io [as 別名]
# 或者: from java.io import File [as 別名]
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
示例14: basename
# 需要導入模塊: from java import io [as 別名]
# 或者: from java.io import File [as 別名]
def basename(path):
"""Return the final component of a pathname"""
path = _tostr(path, "basename")
return asPyString(File(path).getName())
示例15: exists
# 需要導入模塊: from java import io [as 別名]
# 或者: from java.io import File [as 別名]
def exists(path):
"""Test whether a path exists.
Returns false for broken symbolic links.
"""
path = _tostr(path, "exists")
return File(sys.getPath(path)).exists()