I'm trying to write a program to get the artist and album from mp3 files. A simple test run on the Alice in Chanins song Rooster yielded this result -> b'Rooster\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
I set read to read(20) and if you count Rooster plus all the \x00s you'll see that it 20, I'm confused because that chould be 20 bytes and \x00 is 4 bytes. I need to be able to tell the program to stop building the artist string after it hits \x00, how can represent that? Thanks.

Recommended Answers

All 2 Replies

\x00 is byte, not 4, you can strip those with .rstrip('\x00')

>>> b'Rooster\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
'Rooster\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>> _.rstrip('\x00')
'Rooster'
>>> 

I need to make sure that file.read() stops after hitting '\x00' I tried a while loop but it isn't working. Also, does file.read() step one byte forward each time through a loop or do I need to manually move the pointer? Thanks.

#!/usr/bin/env python3

import sys

file = sys.argv[1]
f = open(file, 'rb')

f.seek(-125, 2)
artist = f.read()
char = f.read()
while char is not '\x00':
    char = f.read(1)
    artist = artist + f.read()
print(artist)

f.close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.