0

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!

1
  • open('~/Downloads/This is the file.mp4', 'rb')? Commented Oct 3, 2014 at 0:35

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I knew it was something quite simple. On another note, what I will do with the bytes is splitting them in 1024 byte blocks, and then manipulating that data (appending something to each block). Will I have a problem using "rb" if I am going to manipulate the bytes like this?
@Mandos, I just checked. "rb" is fine for that and should not cause any problems if you try to put the blocks back together.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.