简体   繁体   English

如何在python 2.7中打开视频文件?

[英]How to open a video file in python 2.7?

I am new to python, and I am trying to open a video file "This is the file.mp4" and then read the bytes from that file. 我是python的新手,我正在尝试打开视频文件“ This is the file.mp4”,然后从该文件读取字节。 I know I should be using open(filename, "rb"), however I am not clear about the following things: 我知道我应该使用open(filename,“ rb”),但是我不清楚以下几点:

    • In what directory is python looking for my file when I use open()? 当我使用open()时,python在哪个目录中寻找我的文件? (My file is located in the downloads folder, should I move it? Where? (我的文件位于downloads文件夹中,我应该将其移动吗?在哪里?
    • Is using "rb" the correct way to read the bytes from a file? 使用“ rb”是从文件读取字节的正确方法吗?

So far I tried to open the file and I get this error: 到目前为止,我尝试打开文件,但出现此错误:

IOError: [Errno 2] No such file or directory: 'This is the file.mp4'

I know it is probably an obvious thing to do, however I have looked all over the internet and I still haven't found an answer. 我知道这可能是一件显而易见的事情,但是我在互联网上四处寻找,但仍未找到答案。

Thank you in advance! 先感谢您!

By default, Python opens the file from the current working directory, which is usually the folder where the .py script of the program is located. 默认情况下,Python从当前工作目录打开文件,该目录通常是程序的.py脚本所在的文件夹。

If you move the video file in the same directory as the script, it should work. 如果将视频文件移动到与脚本相同的目录中,则它应该可以工作。

You can also view the current working directory like this: 您还可以像这样查看当前的工作目录:

import os
print os.getcwd()

Also, instead of moving the file, you can just change "This is the file.mp4" to "C:/Users/<username>/Downloads/This is the file.mp4" if you are using Windows 7 and maybe 8. You will have to change the <username> to your computer username. 另外,如果您使用的是Windows 7或8,则可以将"This is the file.mp4"更改为"C:/Users/<username>/Downloads/This is the file.mp4" ,而不是移动文件。您必须将<username>更改为您的计算机用户名。

Wildcards might also work: "~/Downloads/This is the file.mp4" 通配符也可能起作用: "~/Downloads/This is the file.mp4"

Finally, what are you planning to do with the video file bytes? 最后,您打算如何处理视频文件字节? If you want to copy the file to somewhere else, there are modules to do that. 如果要将文件复制到其他位置,可以使用一些模块来完成。

"rb" is a correct way to read bytes of a file. "rb"是读取文件字节的正确方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM