How to get file creation & modification date/times in Python?

I have a script that needs to do some stuff based on file creation & modification dates but has to run on Linux & Windows. What's the best cross-platform way to get file creation & modification date/times in Python? Getting some sort of modification date in a cross-platform way is easy - just call os.path.getmtime(path) and you'll get the Unix timestamp of when the file at

如何在Python中获取文件创建和修改日期/时间?

我有一个脚本需要根据文件创建和修改日期做一些事情,但必须在Linux和Windows上运行。 什么是在Python中获取文件创建和修改日期/时间的最佳跨平台方式? 以跨平台的方式获取某种修改日期非常简单 - 只需调用os.path.getmtime(path) ,就可以得到上次修改path文件时的Unix时间戳。 另一方面,获取文件创建日期依赖于平台和平台,甚至在三大操作系统之间也有所不同: 在Windows上 ,文件的ctime (记录在https://msdn.micr

Python: What OS am I running on?

我需要看看我是否在Windows,Unix等? >>> import os >>> print os.name posix >>> import platform >>> platform.system() 'Linux' >>> platform.release() '2.6.22-15-generic' The output of platform.system() is as follows: Linux: Linux Mac: Darwin Windows: Windows See: platform — Access to underlying platform's identifying data Dang -- lbrandy bea

Python:我在运行什么操作系统?

我需要看看我是否在Windows,Unix等? >>> import os >>> print os.name posix >>> import platform >>> platform.system() 'Linux' >>> platform.release() '2.6.22-15-generic' platform.system()的输出如下所示: Linux: Linux 麦克: Darwin Windows: Windows 请参阅:平台 - 访问底层平台的标识数据 Dang - lbrandy击败了我,但这并不意味着我无法为您提供Vist

How to get full path of current file's directory in Python?

I want to get the current file's directory path. I tried: >>> os.path.abspath(__file__) 'C:\python27\test.py' But how can I retrieve the directory's path? For example: 'C:\python27\' If you mean the directory of the script being run: import os os.path.dirname(os.path.abspath(__file__)) If you mean the current working directory: import os os.getcwd() Note that before an

如何在Python中获取当前文件目录的完整路径?

我想获取当前文件的目录路径。 我试过了: >>> os.path.abspath(__file__) 'C:\python27\test.py' 但是我怎样才能检索目录的路径? 例如: 'C:\python27\' 如果您的意思是正在运行的脚本的目录: import os os.path.dirname(os.path.abspath(__file__)) 如果您的意思是当前的工作目录: import os os.getcwd() 请注意, file之前和之后是两个下划线,而不仅仅是一个。 import os print os.path.dirname(__fi

Import a module from a relative path

How do I import a Python module given its relative path? For example, if dirFoo contains Foo.py and dirBar , and dirBar contains Bar.py , how do I import Bar.py into Foo.py ? Here's a visual representation: dirFoo Foo.py dirBar Bar.py Foo wishes to include Bar , but restructuring the folder hierarchy is not an option. Assuming that both your directories are real Python

从相对路径导入模块

如何根据相对路径导入Python模块? 例如,如果dirFoo包含Foo.py和dirBar ,并且dirBar包含Bar.py ,那么如何将Bar.py导入到Foo.py ? 这是一个视觉表示: dirFoo Foo.py dirBar Bar.py Foo希望包含Bar ,但重构文件夹层次结构不是一种选择。 假设你的两个目录都是真正的Python包(它们里面有__init__.py文件),这里有一个相对于脚本位置包含模块的安全解决方案。 我假设你想这样做,因为你需要在脚本

Open file in a relative location in Python

Suppose python code is executed in not known by prior windows directory say 'main' , and wherever code is installed when it runs it needs to access to directory 'main/2091/data.txt' . how should I use open(location) function? what should be location ? Edit : I found that below simple code will work..does it have any disadvantages ? file="2091sample.txt" path=os.get

在Python中的相对位置打开文件

假设python代码在先前的windows目录中并不知道的地方执行,比如'main',并且在运行代码的任何地方需要访问目录'main / 2091 / data.txt'。 我应该如何使用open(位置)功能? 什么位置应该? 编辑: 我发现,简单的代码下面会工作..它有什么缺点吗? file="2091sample.txt" path=os.getcwd()+file fp=open(path,'r+'); 有了这种类型的事情,你需要小心你的实际工作目录是什么。 例如,您

with os.scandir() raises AttributeError:

An AttributeError is raised when I use the example code from python's documentation (here). The example code is as follows: with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name) The result is an AttributeError : D:Programming>test.py Traceback (most recent call last): File "D:Programmingtest.py",

与os.scandir()引发AttributeError:

当我使用python文档中的示例代码(这里)时引发了AttributeError 。 示例代码如下所示: with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name) 结果是一个AttributeError : D:Programming>test.py Traceback (most recent call last): File "D:Programmingtest.py", line 3, in <module> with os.scandir() as

How to get all of the immediate subdirectories in Python

I'm trying to write a simple Python script that will copy a index.tpl to index.html in all of the subdirectories (with a few exceptions). I'm getting bogged down by trying to get the list of subdirectories. import os def get_immediate_subdirectories(a_dir): return [name for name in os.listdir(a_dir) if os.path.isdir(os.path.join(a_dir, name))] Why has no one mentioned

如何获取Python中的所有直接子目录

我正在尝试编写一个简单的Python脚本,将所有子目录中的index.tpl复制到index.html(有一些例外)。 试图获取子目录列表,我陷入了困境。 import os def get_immediate_subdirectories(a_dir): return [name for name in os.listdir(a_dir) if os.path.isdir(os.path.join(a_dir, name))] 为什么没有人提到glob ? glob允许你使用Unix风格的路径名扩展,并且可以运行几乎所有需要查找多个路径名的东西。

Dynamic traits do not survive pickling

traits_pickle_problem.py from traits.api import HasTraits, List import cPickle class Client(HasTraits): data = List class Person(object): def __init__(self): self.client = Client() # dynamic handler self.client.on_trait_event(self.report,'data_items') def report(self,obj,name,old,new): print 'client added-- ' , new.added if __name__ == '__main__': p = Pers

动态特征不能在酸洗中生存

traits_pickle_problem.py from traits.api import HasTraits, List import cPickle class Client(HasTraits): data = List class Person(object): def __init__(self): self.client = Client() # dynamic handler self.client.on_trait_event(self.report,'data_items') def report(self,obj,name,old,new): print 'client added-- ' , new.added if __name__ == '__main__': p = Pers

rapidly constructing table with primary key

This question already has an answer here: Improve INSERT-per-second performance of SQLite? 10 answers Make sure that your insert statements are executed within a transaction, else an implicit transaction will be opened for every statement which is rather time-consuming. Execution of many consecutive statements should be much faster then.

用主键快速构建表格

这个问题在这里已经有了答案: 提高SQLite的每秒插入性能? 10个答案 确保insert语句在事务中执行,否则将为每个语句打开一个隐式事务,这非常耗时。 然后执行很多连续的语句应该快得多。

Locking sqlite file on NFS filesystem possible?

Let's say there are two python scripts that want to write data to the same table which is stored in an SQLite file using the sqlite3 module. The SQLite-file is stored on an NFS filesystem. In the SQLite-FAQ I read: SQLite uses reader/writer locks to control access to the database. [...] But use caution: this locking mechanism might not work correctly if the database file is kept on an NF

在NFS文件系统上锁定sqlite文件可能吗?

假设有两个python脚本想要将数据写入存储在使用sqlite3模块的SQLite文件中的同一个表中。 SQLite文件存储在NFS文件系统中。 在SQLite-FAQ中,我读到: SQLite使用读写器锁来控制对数据库的访问。 [...]但要小心:如果数据库文件保存在NFS文件系统上,则此锁定机制可能无法正常工作。 这是因为在许多NFS实现中fcntl()文件锁定被破坏。 如果多个进程可能试图同时访问文件,则应避免将NFS数据库文件放在NFS上。 这是否意