我需要为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
命令.我不想要那个解决方案.
我也知道使用ln
或os.walk
可能的解决方法,但我想知道是否可以使用glob.glob
.
对于特定的Exception类型(比如IOError),我如何提取Errnos的完整列表和这样的描述:
Errno 2:没有这样的文件或目录
Errno 122:超出磁盘配额
...
那么我在这里做错了什么?
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) 这很好用:
>>> 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)