Python: Deleting Files with a Specific Extension

Very few things in life come close to the satisfaction that I get when closing out a project and deleting old files. I have been working on automating the deletion of some of my files and here is a quick cleanup python script that you can use too.

First, open your PowerShell or notebook, whichever you are comfortable working in. Then determine where the files that you want to delete are located, and determine if they are a specific type of file you want to delete. Now that you have an essential plan you can begin.

You will need to import a python package that identifies paths on your computer. The package is called Path, and you can pip install it by typing pip install path.py if you do not have it already. One thing I noted is that in some versions of python path you can use a lower case p. I am using Python3 and need to use an uppercase P.
import_path

Then you will need to define the location of the files that you are deleting. Here, I chose to define my function as d but you can call yours whatever you want. Then you will need to list the directory information where your files are located with your specific information. One thing to remember here is make sure you have your opening and closing quotes! I forgot that the first time I was trying this out.

d = Path('C:/Users/name/Desktop/Test_folder/')

Next, apply the method walkfiles to your path d.   For reference, the walk() method comes from the path  package and is able to generate file names in the tree directory on your computer. The purpose of applying this with the astrisk is that you are looking at all types of files. I like to then use a list to show all the files in the folder.

walkfiles list

From there, I created a variable named files and assign it the method of walkfiles and only want to delete Excel files.

defining files

Then you will create a For Loop, which means python will process each line of and end with the print option. What the text is saying is in the files that are defined with the extension of .xsls, you are applying the remove method. Printing the files with the Excel extension will show that the correct things have been deleted.

for_loop

Hope you enjoyed this quick script on deleting. Remember that if you implement this it will delete your files and not simply put them into your trash can!

Continue Reading

Formatting: Leading Zeros

How many times have you exported data and realized a little too late that the leading zeros in constituent IDs were no longer showing up?

I know I have, and much to my chagrin I have had to re-send the file or spend time working through a process of making Excel or CSV format the leading zero appropriately.  I found a way using the Python package, pandas that makes leading zero formatting fairly easy and you don’t have to  add zeros or concat two cells together like you may consider doing in using Excel.

Below is an example data and the the steps that I followed to when I found that zeros where disappearing in my Excel document.

Here is my “data” with the IDs formatted as text so the leading zeros are visible.  When I saved this workbook, I saved it as an Excel file.excel_sample

I then imported pandas in my Jupyter Notebook, as I would normally when loading a dataframe for work and before preparing a file to be used in SPSS.  I wanted to see the first few lines of the file using the DataFrame.head() method and saw that the leading zeros had been dropped.

pd_read_excel

After seeing that the leading zeros were not showing I checked the Excel file to confirm that the IDs were saved as text.  Unfortunately, the ID column was saved as text, so there was nothing to alter there.

My next attempt at resolving the issue involved an reading an Excel file, and attempting to change the ID format to string.  Regrettably that lead to an error that ended with 'The 'dtype' option is not supported with the 'python' engine.

 

excel_error

excel_error_code

I then read documentation about the method of reading CSVs in pandas.  From there, I saw that my earlier code, the object defining the datatype (dtype), was meant to be used with with the function read_csv().  Excited, I saved my example data as a CSV, and reloaded the dataframe and applied the datatype object.  In the example below, you can see that once the dtype is applied, the leading zeros are visible in the dataset. read_csv

After getting past the initial hurdle of finding out how I can format specific documents I found this object pretty quick and efficient. How have you found the best way to work with leading zeros, especially if you can only export into Excel?

Continue Reading

Fitbit Charge HR Teardown

Before I purchased a Fitbit Charge HR I scrutinized over the details of different athletic trackers and their capabilities.  I chose the Fitbit Charge HR because it: tells time, acts like a pedometer, has a heart rate monitor, can track stair climbs, track your sleep, have alarms set, syncs with Bluetooth,  and has various notifications come through like phone calls.  Honestly, it is pretty neat that a watch that do all of that and still be about 1 inch wide.

Earlier this year, my partner had a Charge HR’s wrist strap break and the company sent him a new one.  I am grateful he let me take the old one apart.

Here is the complete teardown of the Fitbit Charge HR:

Tools Needed:

  • Fitbit Charge HR
  • Torx Screwdriver: T2 (I have this set and it is awesome Floureon 38-piece Precision Screwdriver)
  • Spudger
  • Small bowl or container for screws
  • Self Healing mat or light workspace – I use this because my counter is dark and shiny so it is hard to see small parts.

 

Teardown

When you remove your Fitbit and flip it over you see the sensor.  Looking at the corners of the sensor plate you will see 4 torx shaped screws.  Torx screws are six sided star screws and are common in cars, bikes and other electronics.  When created, torx were initially tamper proof because the shape was strange, kind of like the 5 star pentalobe screw that Apple started using in the 2010s.  To remove the torx screws use the size T2 and they will come out right away.  I recommend putting them in a small container (I used a ramekin) so you can find them later.

torxamp

Now that the screws are out take the spudger and push the plastic backing out of the housing case. Using a spugder is easy and it helps ensure that you won’t damage your screwdrivers or accidentally stab your fingers or workspace.  Spudgers are also nylon so you won’t damage your Fitbit or its case if you want to put it back together.

Once I removed circuit board from the Fitbit I glanced back at the sensor, or the part that touches my wrist.  It is somewhat arguable that the fitbit already displays the the most interesting things, because I think the sensor is pretty neat.

fitbit_housing

The sensor is the rectangular part that looks clear.  It is an optical sensor, so basically the same thing that is inside a computer mouse.  The two dots above and below the sensor are infrared LEDs and they blink the green light. To work together, the LEDs send off light into you arm and sensor detects different light levels being reflected back by oxygenated and deoxygenated blood.  The detection in different light measurement is how the pulse is recored. You can purchase a pulse oximeter at a drugstore, and they are found in hospitals – they are the little sensors that are put on your finger to make you look like E.T.  Because pulse oximeters are used in medical devices to also determine oxygen saturation, it is possible that Fitbit is measuring O2 levels in blood and simply not reporting it back, or the calculation between oxygenated and deoxygenated blood is not made to tell.

 

Once you flip over the band, or where the circuit board was, you will see a yellow circuit (flex circuit) that connects the sensor back to the main circuit board.  To the right, there is a black chip.  It is kind of funny that the chip is not on the main circuit board but that is postulated that the chip is an amp to measure the pulse as there is so much other stuff going on inside the Fitbit.

amp

 

Now, looking at the main circuit board, you will see two yellow flex circuits.  One of the flex circuits connects to the sensor, as mentioned before.  The other flex circuit connects to the display, and part you interact with when wearing the device.

back_circut

Next is the battery, which is the biggest part of the electronic, unsurprisingly. Below the battery is the lithium ion battery charger, which regulates charge current. The white plastic circle is an altitude sensor (barometer), which assists in measuring how many flights of stairs you climb.

Flipping over the circuit board is the main side that has more “stuff” on it. This is the slide you see the display. The display is an OLED (organic LED), and uses very little battery, which is important as the device is busy collecting other data. Above it is a Bluetooth antenna—this is how you sync your device and beat the pants off your buddies in challenges! The display and antenna are soldered onto the circuit board on the upper right side, and also held by two additional torx screws. I accidentally tore the circuit board removing the display, so take caution when removing this part. It is possible that this is connected so snugly to provide more stability for the display.

initial_look

Near the vibration motor is a chip that is the OLED power supply. Then there are two shiny chips, one is 8 bit and one is a 32-bit. Because there is so much going on in the Fitbit it is postulated that is the function for having 2 processors: the 8-bit may handle the Bluetooth stuff (coming from the big chip that says NRF) and display, and the big shiny 32-bit chip handles all the data that is being measured.

processor1

Another cool thing that has 2 of the same pieces are the accelerometers. It is possible that there are 2 accelerometers to more accurately estimate all the movement that is happening in your arm.  (Fun fact: accelerometers are in computer mice, too!)

acccelerometer

Hope you enjoyed the teardown!  I know I learned a lot, and I was also interested to be able to see similar parts as I have seen in other devices.

 

Continue Reading

The Little Toaster that Didn’t: A Teardown

toaster

I had been eyeing the toaster in my office kitchenette for weeks before taking the sophisticated appliance home.  I have a thing for toasters, my colleagues from my first job can attest to that.  After all, they had witnessed my rage of not being able to eat toasted bagels for weeks before purchasing me one. Anyways, I took the beaut from my current office home and tried to dismantle the device.  Here’s what I learned:

Toasters come with screws that mean “Stay out!”.   I was able to figure this out because the screw has a very atypical screw head. The Neji-Saurus was of no help and those screw pliers are awesome. Toasters also have other ways to say “Stay out!”, like bits of twisty metal holding plates in place.  The screws and construction of the toaster is probably difficult for a reason.  That reason is because the electrical filaments (nichrome wire), the little metal bits that turn red, carry large amounts of electrical currents through them to make infrared radiation that makes toast!  Sure, this little toaster was unplugged and hasn’t been plugged in since its factory days – but I remained frightened.

Here is what I was able to teardown and learn about.

Here are the stay out screws – I stayed out.

stay_out_screws

Here is a demo of what it looks like when you press down on the white handle.

Here is the circuit board and where I learned the most.

circuit_capacitor_contacts_electromagnet

 

The pencil is touching a metal tab.  When the metal lever is depressed ( that is the part you push down on to make toast), the metal tab connects with the electromagnet, and that is what holds the toast down.  On the other side, you see a white plastic button.  When the lever is pushed down, the white button goes to the metal prongs sticking up.  Those metal prongs are called contacts and they apply power to the nichrome wires that make toast! That is also why there are a bunch of wires coming out the prongs and running to the filaments!

Back to the metal tab.  To the right of the pencil there is a black box with yellow stuff around it.  That black box is the electromagnet.  The top metal part is what the metal tab sticks to and holds the bread down. The yellow bit is the electromagnet.  The circular black thing behind the green circuit is the darkener/ timing capacitor. The timing capacitor is really how long you want your toast to be toasted / how long power is applied to the nichrome wires. Capacitors are basically like little batteries that store  electrical energy.  So once the timer is done, the electromagnet releases the metal tab, and up pops your toast!

Continue Reading