Geronimo Rodriguez
Geronimo Rodriguez

Reputation: 199

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. I know I should be using open(filename, "rb"), however I am not clear about the following things:

    • In what directory is python looking for my file when I use open()? (My file is located in the downloads folder, should I move it? Where?
    • Is using "rb" the correct way to read the bytes from a file?

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!

Upvotes: 0

Views: 3919

Answers (1)

Electron
Electron

Reputation: 308

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.

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.

Wildcards might also work: "~/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.

Upvotes: 1

Related Questions