Hi,

I am reading data from a file. The data is mostly text, but has a small bit of binary in it, as well as a linefeed at the end of every block of text. Can Python read all of it and write it to another file, while not changing the binary or the linefeed?

I have tried looking around online, but couldn't find the answer. Daniweb is on of the best Forums out there, so I am asking it here.
:|

Also, is there anyway in Python that you can test whether or not a line of data is binary?

Recommended Answers

All 9 Replies

Member Avatar for sravan953

If you don't already know:

(identifier)=open("(file.txt)",'r'(or)'w')
(another_identifier)=(identifier).read()
#That's how you get text from a file which is now stored as a String in (another_identifier)
#Now, mess around!

No, I didnt already know, since I started Python 2 days ago, and have never done another language before that...

Thank you though.

I'm not going to check for binary anymore, instead I will test every line in the file against a regular expression. That is the end of the file. After that I will use pickle to dump it to a new file.

What I asked was if there is anyway to check whether or not input received from a file is in binary.

All your post says is how to read from a file.

open it in binary mode. In unix however binary mode isn't needed but using 'wb' or 'rb' instead of 'w' or 'r' doesn't hurt. This should copy the file contents of a file to another no matter the content:

fi=open("filein", 'rb').read()
fo=open("fileout",'wb')
fo.write(fi)
fo.close()

In windows you just gotta use the 'rb'/'wb' flags to read the file contents as binary.

If you read a binary as a text file (mode='r'), you have to be aware that the binary data can inadvertently contain an EOF (End Of File) marker early on, at which point the read would stop. Using a binary read (mode='rb') will prevent this.

Here is a simple way to detect a binary file ...

# use with Python3

def is_binary_file(fname):
    fin = open(fname, "rb")
    data = fin.read()
    fin.close()
    # a typical binary file contains zero value bytes
    if 0 in data:
        print("File %s is binary" % fname)
    else:
        print("File %s is not binary" % fname)

# pick a typical binary file like an image file you have
# in the working directory or give the full file path
fname = 'py.ico'
is_binary_file(fname)

# pick a text file you have in the working directory
fname = 'Zoomer.txt'
is_binary_file(fname)

If you use Python2 the binary read is different, you have to iterate and use ord().

BTW, thread titles like ...
Can Python do this?
I need help
Help

are meaningless and kind of stupid! Use a tiltle that tells the rest of us what you want.

Also, is there anyway in Python that you can test whether or not a line of data is binary?

All data is binary. That's the way the computer does it. I am assuming that you mean bytes that are not text. If you look at an ASCII table like this one http://www.asciitable.com/ it becomes apparent that for the English character set you want to check for anything less than decimal 32 (space) or greater than decimal 126 (~). Python uses ord(character) to covert and I don't know of any way other than converting and checking every byte in the file. Post some code and we can help you with problems

BTW, thread titles like ...
Can Python do this?
I need help
Help
are meaningless and kind of stupid! Use a tiltle that tells the rest of us what you want.

It takes a large amount of self control not to respond with
Can Python do this? (Who are you, Steve Urkel)
I need help (We all do but most of us can't afford a psychiatrist)
Help (Have you fallen and can't get up?)

It takes a large amount of self control not to respond with
Can Python do this? (Who are you, Steve Urkel)
I need help (We all do but most of us can't afford a psychiatrist)
Help (Have you fallen and can't get up?)

The answer to "Can Python do this ?" is usually Yes.
"I need help" often means it's homework due in 2 hours.

LF and CR are actually printable.

This little code will give you a list of printable characters ...

import string

print( list(string.printable) )

All data is binary. That's the way the computer does it. I am assuming that you mean bytes that are not text. If you look at an ASCII table like this one http://www.asciitable.com/ it becomes apparent that for the English character set you want to check for anything less than decimal 32 (space) or greater than decimal 126 (~). Python uses ord(character) to covert and I don't know of any way other than converting and checking every byte in the file. Post some code and we can help you with problems

It takes a large amount of self control not to respond with
Can Python do this? (Who are you, Steve Urkel)
I need help (We all do but most of us can't afford a psychiatrist)
Help (Have you fallen and can't get up?)

I appreciate all the help, guys. Thank you.

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.