Saturday, September 7, 2024

Figuring out if that USB drive is legit

So you just bought USB drives off Temu or some other e-commerce site because it was cheap. You like what you see, 32GB for 99 bucks + shipping, I guess I'll buy five.

But are you really getting a deal?

Mayhaps. But unlike the common folk I don't take such things on face value and  see if we are really getting the real deal. Thankfully, Linux - in this case, PopOS - has the command line tools to check.

So get 1 USB drive to test and we install, F3

$ sudo apt install f3

F3 comes with a command line utility named f3probe, which is used to test USB flash drives for capacity.

But before we run f3probe, we need to figure what block device name the USB drive was given.

$ lsblk

 

As you can see, mine is sdb/sdb1

so

$ sudo f3probe --destructive --time-ops /dev/sdb

Wait for it. BTW, back up if you files in the USB before you do this.

And then be disappointed because your 99 bucks 32GB USB drive is "counterfeit". It's only an 8GB.

Oh well. I hope you didn't buy five. 

 

Ref

  • https://www.linuxbabe.com/command-line/f3-usb-capacity-fake-usb-test-linux

Monday, July 10, 2023

When you need to open 3 (or more) files in Python3

The problem is I needed to open 3 files:

1. CSV source file to read
2. CSV file to write results on after processing the first file
3. Another CSV file that has metadata resulting from processing the 1st CSV that's different from the 2nd

Solution

There are 2 practical ways to do it. There's a 3rd but it has a very specific use case.

As for Python3, you can basically do this using context managers via the 'with' keyword:


with open('a', 'r') as a_file, open('b', 'w') as b_file, open('c', 'wb') as c_file:    
    do_something()

I believe it this also exist in Python 2.7 but you might need to use the contextlib.nested() function

The other approach is when you need to process files sequentially rather than opening all of them at the same time. Useful if you have variable number of files:

for fname in filenames:
    with open(fname) as f:
        # Process f

You can also use this if you would rather keep the results in memory and only write it to the file when done. 

Reference: 

  • https://stackoverflow.com/questions/4617034/how-can-i-open-multiple-files-using-with-open-in-python