我正在尝试在Python2.7中使用作为os.walk的替代。但是,在导入过程中会引发ImportError,如下所示。
Python 2.7.12 (default, Jul 1 2016, 15:12:24)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from scandir import scandir, walk
Traceback
我如何快速计算一个大目录的大小,同时计算python跨平台的所有文件,这是我目前的代码,但它在大文件数量(超过100000)时非常慢:
class filecounter:
def count(self, scandir):
global filescount
global totalsize
if not scandir[-1] == '/' or '\\':
scandir = scandir + '/'
man scandir给出了scandir函数的以下结构:
int scandir(const char *dirp, struct dirent ***namelist,
int (*filter)(const struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **));
假设我定义如下:
struct dirent **namelist;
int len = scandir('.', &namelist, NU
我有一个父dir,它有X个子目录,每个都带有Y txt文件。我想列出所有的txt文件。有办法简化这件事吗?
for entry in os.scandir(DIR):
for subentry in os.scandir(entry.path):
if subentry.is_dir():
for file in os.scandir(subentry.path):
if file.is_file() and file.name.endswith("txt"):
我试图使用scandir打印当前目录中的文件列表。当我试图编译时,我会收到以下错误和警告:
warning: implicit declaration of function ‘scandir’ [-Wimplicit-function-declaration]
error: ‘alphasort’ undeclared (first use in this function)
note: each undeclared identifier is reported only once for each function it appears in
我包括<dirent.h>,据我
内置的python和普通的对象之间有什么区别?我们常说,在python中,一切都是对象。例如,当我在Python3.6中这样做时:
>>> import os, inspect
>>> inspect.getsource(os.scandir)
TypeError: <built-in function scandir> is not a module, class, method, function, traceback, frame, or code object
我有两个问题:
内置函数是一个对象吗?如果不是,这就是getsource抛
我正在尝试使用python查找超过1分钟的文件。有人能帮我一下吗? 我知道我们可以使用subprocess和linux find命令,但我想用python编写它。 with os.scandir(dirss) as listOfEntries:
for entry in listOfEntries:
data=os.path.join(dirss,entry)
if os.stat(data).st_mtime < now + 1:
函数是否处理相对路径?
换句话说,以下两段代码是否等效?
int NUM = scandir("/home/hello/wait/../pqr/../../hello", &LIST, 0, alphasort);
int NUM = scandir("/home/hello", &LIST, 0, alphasort);
如果没有,是否有一些简单的buit-in函数将/home/hello/wait/../pqr/../../hello转换为/home/hello?
for entry in os.scandir(document_dir)
if os.path.isdir(entry):
# some code goes here
else:
# else the file needs to be in a folder
file_path = entry.path.replace(os.sep, '/')
在os.scandir语句中,我很难模拟和path属性。我无法模拟我在单元测试中创建的模拟对象的属性。
with patch("os.scandir") as m
使用Python2.7和scandir,我需要遍历所有的目录和子目录,并且只返回目录的列表。不是文件。沿着路径的子目录的深度可能会有所不同。
我知道os.walk,但是我的目录有200万个文件,因此os.walk会为此放慢速度。
目前,下面的代码适用于我,但我怀疑可能有一种更简单的方法/循环来实现相同的结果,我想知道如何改进它。此外,我的功能的局限性是,它仍然受到我可以进入子目录的深度的限制,也许这是可以克服的。
def list_directories(path):
dir_list = []
for entry in scandir(path):
if entry.is_dir()
我正在尝试编写一个函数,该函数通过使用下面的os.walk()遍历目录树来查找目录。在我的机器上,这需要15秒。 for dir_path, dir_names, filenames in os.walk(os.path.expanduser('~')):
for dir_name in dir_names:
if dir_name == 'some_dir':
path = os.path.join(dir_path, dir_name)
print(path) 我读到os.scandi