在Python中,有许多内置异常可以被各种标准库函数(当然还有其他代码)抛出.由于许多原因,可能会抛出某个异常,您可能想知道它是否因特定原因而被抛出.
例如,在Windows中,如果您尝试在另一个进程锁定文件时移动该文件,您可能会得到PermissionError:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Path\\to\\the\\file'
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我想确定是否PermissionError抛出异常的原因是因为我尝试移动的文件被锁定,我目前通过查看我捕获的异常中的错误消息来执行此操作:
try:
# Move file
os.rename(source_path, dest_path)
except PermissionError as e:
if str(e).find('The process cannot access the file because it is being used by another process') != -1:
# File not unlocked yet; do something, e.g. wait a moment and try again
else:
# Exception thrown for some other reason; do something else
Run Code Online (Sandbox Code Playgroud)
但是,要检查是否 …
如何从 pts 和 time_base 或持续时间获取视频或 rtmp 流中帧的时间戳?多谢!
import av
def init_input(file_name):
global a
container = av.open(file_name)
a = container.duration
return container.decode(video=0)
url = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov"
stream1 = init_input(url)
for frame1 in stream1:
print(frame1.pts)
print(frame1.time_base)
Run Code Online (Sandbox Code Playgroud)
PS:frame.time与实际时间不正确
有人可以向我解释这里发生了什么吗?考虑一下代码
#include <iostream>
int main()
{
int A[2][2] = {{0}};
std::cout << A << std::endl; // First stdout line
std::cout << *A << std::endl; // Second stdout line
std::cout << *(*A) << std::endl; // Third stdout line
}
Run Code Online (Sandbox Code Playgroud)
(试试这里的代码!)
在我看来,A应该是一个2个指向数组的数组,每个指针应包含2个指向ints的指针.但是,在运行代码时,会将以下内容写入stdout:
0x7a665507cf80
0x7a665507cf80
0
Run Code Online (Sandbox Code Playgroud)
对我来说,这似乎是第一个元素的内存地址A(在第一个stdout行上打印)与第一个元素的内存地址相同*A.这怎么可能,考虑到这一点,A并且*A显然是两个不同的阵列(因为解除引用A并*A给出不同的结果)?
对输出的另一种解释是,内存地址0x7a665507cf80要么包含值0x7a665507cf80(即位于该位置的指针 - 在这种情况下A指向自身的点),或者0取决于是否从中访问,A或者*A也不是对我有意义.
我使用 Python 包yfinance获取股票的历史股价(在本例中为特斯拉的股票)。
当我执行以下操作并以一分钟为间隔获取上周的股票价格时:
import yfinance as yf
print(yf.Ticker('TSLA').history(period='7d', interval='1m'))
Run Code Online (Sandbox Code Playgroud)
我明白了
Open High Low Close Volume Dividends Stock Splits
Datetime
2020-12-03 09:30:00-05:00 586.391479 590.975586 585.549988 586.391479 2999806 0 0
2020-12-03 09:31:00-05:00 586.320007 591.919983 586.320007 591.619995 457446 0 0
2020-12-03 09:32:00-05:00 591.820007 591.907104 586.000000 587.492798 324244 0 0
2020-12-03 09:33:00-05:00 586.909973 590.020020 586.799988 588.919983 306530 0 0
2020-12-03 09:34:00-05:00 588.730774 588.919922 584.330017 584.688416 318614 0 0
... ... ... ... ... ... ... ...
2020-12-11 10:20:00-05:00 613.155029 614.059998 612.770020 …Run Code Online (Sandbox Code Playgroud) 鉴于以下课程
template <class T>
class A {
static const B<T> member;
};
Run Code Online (Sandbox Code Playgroud)
如何单独member为每个类实例化A<T>?
我正在尝试在 Linux 上构建 glfw 库作为共享库。我首先克隆git 存储库。然后,根据此编译指南,我创建了一个名为glfw-buildin the repo 的文件夹并cd放入其中。从那里运行cmake ..给出输出
-- The C compiler identification is GNU 4.8.4
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create …Run Code Online (Sandbox Code Playgroud) 我有以下Python 3代码:
from tqdm import tqdm
print("Before")
for _ in tqdm(range(10)): pass
print("After")
Run Code Online (Sandbox Code Playgroud)
我希望得到以下输出到终端:
Before
100%|##########| 10/10 [00:00<?, ?it/s]
After
Run Code Online (Sandbox Code Playgroud)
但是,我得到的是:
100%|##########| 10/10 [00:00<?, ?it/s]
Before
After
Run Code Online (Sandbox Code Playgroud)
即打印输出相对于我的代码以错误的顺序结束.我也试过在调用sys.flush之前和之后调用print,只是为了得到以下输出:
Before
100%|##########| 10/10 [00:00<?, ?it/s]After
Run Code Online (Sandbox Code Playgroud)
另外,改变print对tqdm.write行为没有任何影响.
为什么它以这种意想不到的方式表现?
编辑:这个问题是关于在tqdm循环之前或之后使用print函数的特定情况.还有其他类似的问题是关于在 tqdm循环中打印消息,而在这种情况下并非如此.
我有以下测试代码:
import sys
from PySide.QtGui import *
app = QApplication(sys.argv)
widget = QWidget()
painter = QPainter(widget)
Run Code Online (Sandbox Code Playgroud)
在创建QPainter对象时,我收到错误消息:
QPainter::begin: Paint device returned engine == 0, type: 1
Run Code Online (Sandbox Code Playgroud)
为什么?
根据subprocess.Popen的Python 3文档,类构造函数接受一个可选参数text(它应该控制文件对象stdin,stdout和stderr是否以文本模式打开).
但是,当我尝试设置对象的text=true构造时Popen,我得到错误
Failed: TypeError: __init__() got an unexpected keyword argument 'text'
Run Code Online (Sandbox Code Playgroud)
当我查看源代码(我使用的是Python 3.6.4)时,构造函数不带参数text.这里发生了什么?为什么文档说构造函数text在subprocess.py我的版本中没有可选参数?