我有一个生成系列的生成器,例如:
def triangleNums():
'''generate series of triangle numbers'''
tn = 0
counter = 1
while(True):
tn = tn + counter
yield tn
counter = counter + 1
Run Code Online (Sandbox Code Playgroud)
在python 2.6中,我可以进行以下调用:
g = triangleNums() # get the generator
g.next() # get next val
Run Code Online (Sandbox Code Playgroud)
但是在3.0中,如果我执行相同的两行代码,我会收到以下错误:
AttributeError: 'generator' object has no attribute 'next'
Run Code Online (Sandbox Code Playgroud)
但是,循环迭代器语法在3.0中有效
for n in triangleNums():
if not exitCond:
doSomething...
Run Code Online (Sandbox Code Playgroud)
我还没有能找到解释3.0行为差异的任何东西.
我只是python中的新手,对于noobish问题感到抱歉
>>> import os
>>> os.listdir("/home/user/Desktop/1")
['1.txt', '2', '3.txt']
>>> os.path.isfile("/home/user/Desktop/1/1.txt")
True
>>> for i in os.listdir("/home/user/Desktop/1"):
... print(os.path.isfile(i))
...
False
False
False
>>>
Run Code Online (Sandbox Code Playgroud)
其中两个是文件,那么当它应该是True时输出为False的原因是什么?
假设我正在编写代码pathlib,并且我想迭代目录同一级别中的所有文件。
我可以通过两种方式做到这一点:
p = pathlib.Path('/some/path')
for f in p.iterdir():
print(f)
Run Code Online (Sandbox Code Playgroud)
p = pathlib.Path('/some/path')
for f in p.glob('*'):
print(f)
Run Code Online (Sandbox Code Playgroud)
其中一种选择是否更好?
如何使用nodejs纯JavaScript或包计算目录中的文件数?我想做这样的事情:
或者在bash脚本中我会这样做:
getLength() {
DIRLENGTH=1
until [ ! -d "DIR-$((DIRLENGTH+1))" ]; do
DIRLENGTH=$((DIRLENGTH+1))
done
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试获取特定目录中的文件列表并计算目录中的文件数.我总是得到以下错误:
WindowsError: [Error 3] The system cannot find the path specified: '/client_side/*.*'
Run Code Online (Sandbox Code Playgroud)
我的代码是:
print len([name for name in os.listdir('/client_side/') if os.path.isfile(name)])
Run Code Online (Sandbox Code Playgroud)
我按照这里给出的代码示例.
我在Pyscripter上运行Python脚本,目录/ client_side/do存在.我的python代码在根文件夹中,并有一个名为"client_side"的子文件夹.有人可以帮我解决这个问题吗?
有这个:
import os, os.path
print len([name for name in os.listdir(os.path.expanduser("~")) if os.path.isfile(name)])
Run Code Online (Sandbox Code Playgroud)
但它总是返回0.如何修改此返回文件的数量?
谢谢
python ×4
directory ×1
file ×1
iteration ×1
javascript ×1
listdir ×1
node.js ×1
pathlib ×1
python-2.7 ×1
python-3.x ×1