我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用os.path.getmtime()。
def open(self): """ Setup the internal structure. NB : Call this function before extracting data from a file. """ if self.file : self.file.close() try : self.file = open(self.path, 'rb') except Exception as e: raise Exception("python couldn't open file %s : %s" % (self.path, e)) self.file_size = path.getsize(self.file.name) self.creation_date = datetime.fromtimestamp(path.getctime(self.file.name)) self.modification_date = datetime.fromtimestamp(path.getmtime(self.file.name)) self.nomenclature = self.get_nomenclature() self.factory = self.get_factory() self.layout = self.create_layout()
def get_source(self, environment, template): pieces = split_template_path(template) for searchpath in self.searchpath: filename = path.join(searchpath, *pieces) f = open_if_exists(filename) if f is None: continue try: contents = f.read().decode(self.encoding) finally: f.close() mtime = path.getmtime(filename) def uptodate(): try: return path.getmtime(filename) == mtime except OSError: return False return contents, filename, uptodate raise TemplateNotFound(template)
def get_source(self, environment, template): pieces = split_template_path(template) p = '/'.join((self.package_path,) + tuple(pieces)) if not self.provider.has_resource(p): raise TemplateNotFound(template) filename = uptodate = None if self.filesystem_bound: filename = self.provider.get_resource_filename(self.manager, p) mtime = path.getmtime(filename) def uptodate(): try: return path.getmtime(filename) == mtime except OSError: return False source = self.provider.get_resource_string(self.manager, p) return source.decode(self.encoding), filename, uptodate
def get_outdated_docs(self): for docname in self.env.found_docs: if docname not in self.env.all_docs: yield docname continue targetname = path.join(self.outdir, self.file_transform(docname)) try: targetmtime = path.getmtime(targetname) except Exception: targetmtime = 0 try: srcmtime = path.getmtime(self.env.doc2path(docname)) if srcmtime > targetmtime: yield docname except EnvironmentError: # source doesn't exist anymore pass
def test_filestorage(sourcedir, tmpdir): """Test file storage copy.""" fsrc = join(sourcedir, 'simple/package.json') fdst = join(tmpdir, 'simple/package.json') fs = FileStorage(sourcedir, tmpdir) # File is copied assert not exists(fdst) fs.run() assert exists(fdst) # File is *not* copied (not modified) mtime = getmtime(fdst) assert mtime > getmtime(fsrc) fs.run() assert getmtime(fdst) == mtime # File is copied (source was modified) time.sleep(1) utime(fsrc, None) assert mtime < getmtime(fsrc) fs.run() assert getmtime(fdst) >= getmtime(fsrc)
def writeListToFile(filename, _list): # pragma: no cover "Well... writes '_list' to 'filename'. This is for testing only" _logger.debug("Writing to %s", filename) open(filename, mode='w').write( '\n'.join([str(x) for x in _list])) mtime = p.getmtime(filename) time.sleep(0.01) if onWindows(): cmd = 'copy /Y "{0}" +,,{0}'.format(filename) _logger.debug(cmd) subp.check_call(cmd, shell=True) else: subp.check_call(['touch', filename]) for i in range(10): if p.getmtime(filename) != mtime: break _logger.debug("Waiting...[%d]", i) time.sleep(0.1)
def make_image(path_to, filename): try: data = await df_multi_reader(filename=join(path_to, filename)) if len(data.index) > 0: info = name_deconstructor(filename=filename, t="s") info["broker"] = slugify(info["broker"]).replace('-', '_') info["direction"] = "longs" image_filename = filename_constructor(info=info, folder="meta") data = data.loc[data['CLOSE'] != 0] if (not isfile(image_filename)) | (datetime.fromtimestamp(getmtime(image_filename)) < \ (datetime.now() - timedelta(days=30))): await make_strat_image(info=info, data=data) except Exception as err: print(colored.red("At making images {}\n".format(err)))
def qindex_heatmap(broker): try: info = {"broker": broker, "symbol": "AI50", "period": "1440", \ "system": "AI50", "direction": "longs"} filename = join(settings.DATA_PATH, 'portfolios', '{}_qndx'.format(broker)) image_filename = filename_constructor(info=info, folder="heatmap") data = await df_multi_reader(filename=filename) info["direction"] = 1 returns = await convert_to_perc(data=data.last('108M').LONG_PL, info=info) if not returns is None: returns.columns = ['LONG_PL'] if (not isfile(image_filename)) | (datetime.fromtimestamp(getmtime(image_filename)) < \ (datetime.now() - timedelta(days=30))): await save_qindex_heatmap(data=returns, image_filename=image_filename) await make_yearly_returns(returns=returns, info=info) except Exception as err: print(colored.red("At qindex_heatmap {}".format(err)))
def __init__(self, filename, target_dir): self.filename = filename self.target_dir = target_dir self.thumbloc = .5, .5 self.extract_docstring() with open(filename, "r") as fid: self.filetext = fid.read() outfilename = op.join(target_dir, self.rstfilename) # Only actually run it if the output RST file doesn't # exist or it was modified less recently than the example if (not op.exists(outfilename) or ((op.getmtime(outfilename) < op.getmtime(filename)))): self.exec_file() else: print("skipping {0}".format(self.filename))