DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Start Coding With Google Cloud Workstations
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Why I Started Using Dependency Injection in Python
  • Reinforcement Learning for AI Agent Development: Implementing Multi-Agent Systems

Trending

  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  • A Modern Stack for Building Scalable Systems
  • Solid Testing Strategies for Salesforce Releases
  1. DZone
  2. Coding
  3. Languages
  4. Python and Automation: A Perfect Combo

Python and Automation: A Perfect Combo

Python has some advantages in automation over other languages due to its wide range of automation tools, which let you automate many daily tasks in minutes.

By 
Nikita Vasilev user avatar
Nikita Vasilev
·
Feb. 07, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
8.2K Views

Join the DZone community and get the full member experience.

Join For Free

Python is one of the most popular languages among data scientists, researchers, and academics. It systematically tops the ladder on TIOBE, Stack Overflow, and GitHub, being the rockstar among other coding languages. It has a rich library of tools that can be applied to solve various problems in the field of automation. Indeed, Python compares favorably in its ability to automate virtually any process. 

Python also has a lot of built-in libraries. Since many services provide their data via APIs, you will have the ability to write scripts to solve tasks without deep programming knowledge.

But although it seems basic compared to such giants as Java, C, C++, C#, and others, it can still present challenges for learners. 

Automate With Python Like a Hero 

Automation is a very important topic in the software development world and everyday life. It is also an arena where Python has some distinct advantages over other programming languages thanks to a wide range of automation tools. Just think of the tasks you do on a daily basis like summing up numbers, sending emails, copying texts, etc. All these can be automated with Python tools in mere minutes (provided you know the secret words, of course). And I will prove this point further in the post.

So let's imagine you need to help someone automate the process — it happens a lot, I guess. But as is often the case, the person asking for help doesn’t have any programming background or they don’t have the right to put Python on a Windows-based work computer. Again, it’s often the case.

Let's also imagine the task as a shared file open to multiple users. They can all store data here except for executable files (e.g., .bat and .exe). And of course, everyone tends to break this rule.

Let’s type a path: search_path = "/Users/physboy/Article" 

We’ll use the os library.

The walk() function of the os module generates file names in the directory tree, traversing the tree from top to bottom or bottom to top. It also generates a 3-tuple (dirpath, dirnames, and filenames) for each directory in the tree with the root at the top of the directory, including the top itself.

  • dirpath is a string, the path to the directory.
  • dirnames is a list of subdirectory names in dirpath, excluding the special characters "." and "..".
  • filenames is a list of filenames in the dirpath (not directories).
  • os.path.join is a function that joins path components. It is also a nested module in the os module, and implements some useful functions for working with paths (hence, you can play around with it by adding extensions like joining one).

The main advantage of using the os.path module is that it allows the code to remain compatible with all operating systems since it uses a delimiter that matches the platform it runs on. 

It also lets you access the file system in a very easy way. In general, you can use the os module to get information about files and directories and to change the current directory.

Then a function that returns all the files in the directory will look like this:

function to return all the files in the directory

Let's write an auxiliary function (meaning it does not provide functionality on its own) that converts datetime to str:

Auxiliary function

Here’s a function that defines the time a file was created (Windows) and the time when a file was last modified (Unix):

function for time file was created

Here’s a function that defines the time of the last file change, in seconds:

function for last file change time

And the function that defines the file extension:

Function to define file extension

According to network share protection rules, each team creates a subdirectory with their team name.

We will define it by using the function that defines the team name. It assumes that the subdirectory has a team folder.

os.path.relpath defines the path relative to the "start" directory.

example of os.path.relpath

os.path.getsize defines the size in KB.

The function that returns the file size in MB:

Function for file size in MB

Let's put all the files in a DataFrame for easier workflow:

Files listed in dataframe

Let's calculate the information for each file for which we have written the functions:

Calculating information for each file

output

Since we have some restricted files, we will let you remove them automatically to prevent manual removal.

Now let’s count them and remove them using the function os.remove:

Using os.remove function

Now, we need Python installed to get off the ground. Let’s run the python run.py command at the command line.

Now let’s say you’ve overcome your fear of programming and boom… There is another problem — you don’t have the right to install the software.

But each problem has its solution. In this case, our magic wand is the PyInstaller library. The PyInstaller bundles the Python application and all its dependencies into a single executable package. The user can run the application without installing the Python interpreter or any modules. PyInstaller supports Python 2.7 and Python 3.3+ and libraries like PyQt, Django, wxPython, and others.

So, here, we can convert a Python file into an .exe that will run on its own — "pyinstaller -F run.py"

Now all we have to do is to combine it all into one run.py file.

Combining it all into one file

Combining it all into one file, cont.

The Bottom Line

Python is a great language for automating and it's one I bet on for a whole range of tasks, including automation. To demonstrate the breadth of its powers, Python can be used to automate email sorting, replying to, sending HTTP requests, and among other things. Even if you're a newbie, this coding language comes complete with a rich library of packages and ready-to-use automation components. Overall, Python will help you automate the mundane stuff while saving you time and effort.

Python (language) File system

Opinions expressed by DZone contributors are their own.

Related

  • Start Coding With Google Cloud Workstations
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Why I Started Using Dependency Injection in Python
  • Reinforcement Learning for AI Agent Development: Implementing Multi-Agent Systems

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!