小编jur*_*eza的帖子

`os.symlink` vs`ln -s`

我需要为dir2中的dir1(文件或目录)的每个项创建一个符号链接.dir2已经存在且不是符号链接.在Bash中,我可以通过以下方式轻松实现:

ln -s /home/guest/dir1/* /home/guest/dir2/

但是在python中使用os.symlink我收到一个错误:

>>> os.symlink('/home/guest/dir1/*', '/home/guest/dir2/')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 17] File exist
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用os.symlink并运行subprocess命令.我不想要那个解决方案.

我也知道使用lnos.walk可能的解决方法,但我想知道是否可以使用glob.glob.

python linux bash symlink

38
推荐指数
3
解决办法
5万
查看次数

如何在python中获取异常类型的错误号列表(Errno)?

对于特定的Exception类型(比如IOError),我如何提取Errnos的完整列表和这样的描述:

Errno 2:没有这样的文件或目录

Errno 122:超出磁盘配额

...

python exception

8
推荐指数
3
解决办法
6709
查看次数

使用input()时的NameError

那么我在这里做错了什么?

answer = int(input("What is the name of Dr. Bunsen Honeydew's assistant?"))
if answer == ("Beaker"):
    print("Correct!")
else:
    print("Incorrect! It is Beaker.")
Run Code Online (Sandbox Code Playgroud)

但是,我只能得到

  Traceback (most recent call last):
  File "C:\Users\your pc\Desktop\JQuery\yay.py", line 2, in <module>
    answer = int(input("What is the name of Dr. Bunsen Honeydew's assistant?"))
  File "<string>", line 1, in <module>
      NameError: name 'Beaker' is not defined
Run Code Online (Sandbox Code Playgroud)

python input python-2.7

3
推荐指数
1
解决办法
445
查看次数

生成器卡在列表理解中的无限循环中?

这很好用:

>>> def my_range(stop):
    i = 0
    while i < stop:
        yield i
        i += 1

>>> [k for k in my_range(10) if k < 5]
[0, 1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)

现在我修改我的发电机:

>>> def my_range():
    i = 0
    while True:
        yield i
        i += 1

>>> result = []
>>> for k in my_range():
    if k < 5:
        result.append(k)
    else:
        break


>>> print(result)
[0, 1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)

现在,为什么这个陷入无限循环?即使我有k <5.生成器是否应该只在下一次被调用时迭代?

>>> [k for k in my_range() if k < 5]
Run Code Online (Sandbox Code Playgroud)

python list-comprehension generator

0
推荐指数
2
解决办法
1712
查看次数