Hi Ajinkya,

This is my two bits of understanding with files. The function <file_descriptor>.write(<type> object) takes a string type of object. In this case, trying to push in a variable of type integer would obviously throw an error.

With regards to your mail, we have to take into consideration that the following are your requirements:
1. Write the integer to the file
2. Write the integer as is in the file

Taking into consideration the first aforementioned point, the integer could be written in numerous ways. I am most acquainted with Python 2.x and in 2.x there are a few ways to write integers. For example, the following is one of the ways:

<file_descriptor>.write('%d' % <integer_variable>)

The aforementioned is old-style formatting and it is indeed a stylistic choice. Some prefer the str(<non_string_variable>) as compared to the above method. Apart from this, there is also the way of writing in the following way:

with open(<string_filepath_filename>, 'w') as f:
        print >>f, <integer_variable>

[NOTE : This specific one mentioned above will only work with Python 2.x]

One more way is as follows(by packing the entire thing as a struct):

with open(<string_filepath_filename>, 'wb') as f:
        if isinstance(<integer_value>, int):
                f.write(struct.pack('i', <integer_value>))

The struct function can be found in the struct module.

Now taking into consideration the second part where the requirement is to write the integer as is in the file, the representation of string and integer is indeed different. One way, in my opinion, is to write the value in binary. This can be done using the bin(<integer_variable>) function. Some have suggested that writing the integers as fixed width binary strings would be best and hence suggested the following way to convert them:

'{0:032b}'.format(<integer_variable>) <- this will give a 32 character wide binary number with '0' as filler.

In my personal opinion, unless you are doing any processing in which the integer value has to be specifically of any width or representation, using the str(<non_string_variable>) should be fine.

Just one other suggestion, it is best considered to open the files using the with directive as compared to using the open() function.

Please feel free to correct me/add to this if I have missed anything.

--
Sayantan Bhattacharya
https://artemisfowl.github.io
Sent from Alpine

On Thu, 26 Oct 2017, Ajinkya Bobade wrote:

Hello,
This is my first question on this forum point me in a right way if I posted
in a wrong place. That aside I am trying to write a file with '.bag
' extension to sd card( .bag is used in Ross programming).

I wrote a code to write a simple integer to disk as shown


file = open("/path/to/file", 'w')
x = 1

while True:
   x = x + 1
   print(x)
   file.write(str(x))
   file.flush()

in this code I could not write file.write(x). Why is this?If I want to
store a file(' as it is') on this disk without converting it into string
what should I do?
_______________________________________________
BangPypers mailing list
BangPypers@python.org
https://mail.python.org/mailman/listinfo/bangpypers

_______________________________________________
BangPypers mailing list
BangPypers@python.org
https://mail.python.org/mailman/listinfo/bangpypers

Reply via email to