Sitemap

Data Extraction with Python & VS Code

2 min readSep 10, 2024

--

Press enter or click to view image in full size

Scenario:

Your company needs to learn about the files located on various machines. You have been asked to build a script that extracts information such as the name and size of the files in the current working directory and stores it in a list of dictionaries

Solution:

Create a script that generates a list of dictionaries about files in the working directory. Then print the list.

Get John Labossiere’s stories in your inbox

Join Medium for free to get updates from this writer.

Prerequisites:

  • VS Code
  • Python installed
  • GitHub account

Script

Press enter or click to view image in full size

# Importing the os module
import os

# Get the current directory path
current_dir = os.getcwd()

# List all items (files and directories) in the current directory
all_items = os.listdir(current_dir)

# Start an empty list to store information about files
file_info_list = []

# Loop through the items in the current directory
for item in all_items:
# Get full path for each item
full_path = os.path.join(current_dir, item)

# Check if the item is a file (not a directory)
if os.path.isfile(full_path):
# Create a dictionary with the file's path and size
file_info = {
'file_path': full_path,
'file_size': os.path.getsize(full_path)
}

# Add file info to the list
file_info_list.append(file_info)

# Print each file's info
for info in file_info_list:
print(info)

Push code to Github

Now push the code from VS Code to Github. It took some troubleshooting to figure out how to complete this step, but I was glad to figure it out using the terminal commands below.

# Initialie a New Git Repository
git init

# Add File
git add Data-Extraction.py or (file name)

# Commit Changes
git commit -m "initial commit, adding Data-Extraction.py"

# Check the Current Branch. Which was main.
git branch

# Add a Remote Repository
git remote add origin https://github.com/john-laboss19/Data-Extraction.py.git

# Push Your Changes to Github
git push -u origin main
Press enter or click to view image in full size

Github Repository:

You can view my script Data-Extraction.py using the Github link below.

https://github.com/john-laboss19/Data-Extraction.py

--

--