writing file with random extension to sd card using python

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

writing file with random extension to sd card using python

Ajinkya Bobade
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
[hidden email]
https://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|

Re: writing file with random extension to sd card using python

sayantan bhattacharya
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
> [hidden email]
> https://mail.python.org/mailman/listinfo/bangpypers
>
_______________________________________________
BangPypers mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|

Re: writing file with random extension to sd card using python

Sushrut Deshpande
In reply to this post by Ajinkya Bobade
I think you shud open file as binary write and try to write the integer after converting it to utf-8

Best Regards,
Sushrut Deshpande
Founder - Qvestron Systems Pvt. Ltd.,
[hidden email]
+91 9513333160



On 27 Oct 2017 00:20, at 00:20, Ajinkya Bobade <[hidden email]> 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
>[hidden email]
>https://mail.python.org/mailman/listinfo/bangpypers
_______________________________________________
BangPypers mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|

Re: writing file with random extension to sd card using python

Krishnan Nagarajan
Hi,
Refer :
https://stackoverflow.com/questions/27238680/writing-integers-in-binary-to-file-in-python


With Python 3 you can do the following:

i = 6277101735386680763835789423176059013767194773182842284081with
open('out.bin', 'wb') as file:
    file.write((i).to_bytes(24, byteorder='big', signed=False))
with open('out.bin', 'rb') as file:
    j = int.from_bytes(file.read(), byteorder='big')
print(j)

Output:

$ python3 tiny.py6277101735386680763835789423176059013767194773182842284081


What this tells us :

While writing int (or any data including string - as UTF) as binary data to
a file, you need to conver (and encode) it to bytes;
In this case we need to specify the width as well.

While reading such binary data from the file, you need to correspondingly
decode (interpret) the data while converting into python data type (int /
even string / unicode).
Again sidth needs to be specified here to disctate how many bytes to use to
convert to an int.







On Fri, Oct 27, 2017 at 7:11 AM, Sushrut Deshpande <[hidden email]>
wrote:

> I think you shud open file as binary write and try to write the integer
> after converting it to utf-8
>
> Best Regards,
> Sushrut Deshpande
> Founder - Qvestron Systems Pvt. Ltd.,
> [hidden email]
> +91 9513333160
>
>
>
> On 27 Oct 2017 00:20, at 00:20, Ajinkya Bobade <[hidden email]>
> 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
> >[hidden email]
> >https://mail.python.org/mailman/listinfo/bangpypers
> _______________________________________________
> BangPypers mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/bangpypers
>
_______________________________________________
BangPypers mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|

Re: writing file with random extension to sd card using python

Ajinkya Bobade
Thank you for inputs really appreciate it. I was able to get an
understanding of whats going on here in code. Moving forward, I have a more
detailed question. In the following code, I want to capture print('Recording
GPS Position...')  on sd card, for now, this is printing on terminal
directly I want to capture this runtime process from terminal and store on
sd card as it is being executed how do I do this. Also in general what is a
way to capture and store processes in runtime from terminal and store on sd
card(note I do want to store processes as it is being executed and not
after its execution is done)

import sys
import os
import time
import subprocess, shlex
import signal
import serial
import psutil
from subprocess import Popen, PIPE


def recording():
    flag = 0
    ser = serial.Serial('/dev/ttyACM0', 921600, timeout=1)
    ser.flushOutput()

    # ROSBAG Recordings (Shell commands that execute the messages on
the terminal)

    messages = 'rosbag record -o GPS_Position.bag
dji_sdk/gps_position', 'rosbag record -o IMU_Data.bag dji_sdk/imu',
'rosbag record -o Attitude.bag dji_sdk/attitude', 'rosbag record -o
Velodyne_Packets.bag velodyne_packets', 'rosbag record -o
Velodyne_Points.bag velodyne_points',  # rosbag record -o Velocity.bag
dji_sdk/velocity'

    while flag == 0:
        try:
            args1 = shlex.split(messages[0])  # messages[0] = rosbag
record -o GPS_Position.bag dji_sdk/gps_position
            #print (args1)
            p_1 = subprocess.Popen(args1, stdout=PIPE)
            print('Recording GPS Position...')
            p_1.stdout.flush()


On Thu, Oct 26, 2017 at 10:51 PM, Krishnan Nagarajan <
[hidden email]> wrote:

> Hi,
> Refer :
> https://stackoverflow.com/questions/27238680/writing-
> integers-in-binary-to-file-in-python
>
>
> With Python 3 you can do the following:
>
> i = 6277101735386680763835789423176059013767194773182842284081with
> open('out.bin', 'wb') as file:
>     file.write((i).to_bytes(24, byteorder='big', signed=False))
> with open('out.bin', 'rb') as file:
>     j = int.from_bytes(file.read(), byteorder='big')
> print(j)
>
> Output:
>
> $ python3 tiny.py6277101735386680763835789423
> 176059013767194773182842284081
>
>
> What this tells us :
>
> While writing int (or any data including string - as UTF) as binary data to
> a file, you need to conver (and encode) it to bytes;
> In this case we need to specify the width as well.
>
> While reading such binary data from the file, you need to correspondingly
> decode (interpret) the data while converting into python data type (int /
> even string / unicode).
> Again sidth needs to be specified here to disctate how many bytes to use to
> convert to an int.
>
>
>
>
>
>
>
> On Fri, Oct 27, 2017 at 7:11 AM, Sushrut Deshpande <[hidden email]>
> wrote:
>
> > I think you shud open file as binary write and try to write the integer
> > after converting it to utf-8
> >
> > Best Regards,
> > Sushrut Deshpande
> > Founder - Qvestron Systems Pvt. Ltd.,
> > [hidden email]
> > +91 9513333160
> >
> >
> >
> > On 27 Oct 2017 00:20, at 00:20, Ajinkya Bobade <
> [hidden email]>
> > 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
> > >[hidden email]
> > >https://mail.python.org/mailman/listinfo/bangpypers
> > _______________________________________________
> > BangPypers mailing list
> > [hidden email]
> > https://mail.python.org/mailman/listinfo/bangpypers
> >
> _______________________________________________
> BangPypers mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/bangpypers
>
_______________________________________________
BangPypers mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/bangpypers