Use of docstrings in Python programming

Getting started using Python on Windows for scripting and automation

  • 9 minutes to read

Below is a step-by-step guide to setting up your developer environment and getting started using Python to script and automate file system operations on Windows.

Note

This article describes how to set up your environment to use some of the useful libraries in Python that can automate tasks across platforms, such as: B. Browsing the file system, accessing the Internet, evaluating file types, etc. with a Windows-centric approach. For Windows-specific operations, see ctypes, a C-compatible foreign function library for Python, winreg, functions that expose the Windows registration API for Python, and Python / WinRT to access Windows Runtime APIs from Python to enable.

Setting up the development environment

If you are using Python to write scripts that perform file system operations, you should install Python from the Microsoft Store. When installing from the Microsoft Store, the basic Python3 interpreter is used, but in addition to providing automatic updates, your PATH settings are set up for the current user (so no administrator access is required).

If you are using Python for the Web development on Windows, we recommend a different setup using the Windows Subsystem for Linux. For a walkthrough, see our guide: Getting Started Using Python for Web Development on Windows. If you're new to Python, check out our guide: Getting Started Using Python on Windows for Beginners. For some advanced scenarios (such as when you need to access or modify installed Python files, make copies of binary files, or build Python DLLs directly), consider a specific Python release directly from python.org download or install an alternative, e.g. B. Anaconda, Jython, PyPy, WinPython, IronPython, etc. This is only recommended if you are an accomplished Python programmer and have specific reasons for choosing an alternative implementation.

Install Python

To install Python using the Microsoft Store:

  1. Go to the menu begin (lower left Windows icon), enter “Microsoft Store” and select the link to open the store.

  2. Once the store is open, select in the top right menu Search and enter “python”. Open “Python 3.7” in the results under “Apps”. Choose Recall out.

  3. After Python completes the download and installation process, open Windows PowerShell from the menu begin (lower left Windows symbol). Once PowerShell is open, enter to confirm that Python3 has been installed on your computer.

  4. The Microsoft Store installation of Python includes PIP, the default package manager. PIP allows you to install and manage additional packages that are not part of the standard Python library. Enter to confirm that you also have PIP available to install and manage packages.

Install Visual Studio Code

When using VS Code as a text editor / Integrated Development Environment (IDE), you can use IntelliSense (a code completion aid), Linting (helps avoid bugs in your code), Debug Assistance (helps you spot bugs in your code execution), code snippets (templates for small reusable code blocks) and unit tests (testing the code interface with different input types).

Download VS Code for Windows and follow the installation instructions: https://code.visualstudio.com.

Install the Microsoft Python extension

You need to install the Microsoft Python extension to take advantage of the VS Code support features. additional Information

  1. Open the VS Code Extensions window by clicking CTRL + SHIFT + X Press (or navigate in the menu to view > Extensions).

  2. Enter in the field above Search for extensions in the Marketplace The following: python.

  3. Find the extension Python (ms-python.python) from Microsoftand choose the green button To install out.

Open the built-in PowerShell terminal in VS Code

VS Code has a built-in terminal that you can use to open a Python command line with PowerShell. This establishes a seamless workflow between the code editor and the command line.

  1. Open the Terminal in VS Code, select Show > terminal or alternatively use the shortcut CTRL + ` (with the grave accent).

    Note

    The default terminal should be PowerShell. However, if you need to change it, use CTRL + SHIFT + Pto access the command palette. Give Terminal: Select standard shell and a list of terminal options with PowerShell, Command Prompt, WSL, and so on will appear. Select the option you want and enter CTRL + SHIFT + ` (with the backseat) to create a new terminal.

  2. Open Python in your VS Code terminal by typing:.

  3. Try the Python interpreter by typing:. Python returns your "Hello World" statement.

  4. To quit Python, you can type, or "CTRL + Z".

Install Git (optional)

If you intend to collaborate on your Python code with others or to host the project in an open source location (like GitHub), VS Code supports version control with Git. The Source Control tab in VS Code will list all Changes tracked and common Git commands ("Add", "Commit", "Push", "Pull") integrated directly into the user interface. You must first install Git to enable the Source Control section.

  1. Download Git for Windows from the git-scm website and install Git.

  2. An installation wizard is included that asks you a series of questions about the settings for the Git installation. You should use all of the default settings unless you have a specific reason to change something.

  3. If you've never used Git before, you can use GitHub guides to get started.

Sample script to display the structure of your file system directory

General system administration tasks can take a long time, but you can use a Python script to automate these tasks so that you don't have to invest time at all. Python can e.g. For example, you can read the contents of your computer's file system and perform operations such as outputting an outline of your files and directories, moving folders from one directory to another, or renaming hundreds of files. Typically, these tasks can take time to complete when performed manually. Use a python script instead!

Let's start with a simple script that iterates through a directory structure and displays the directory structure.

  1. Open PowerShell from the menu begin (lower left Windows symbol).

  2. Create a directory for the project: and then open the directory:.

  3. Create some directories to use with our example script:

  4. Create some files in these directories for use with our script:

  5. Create a new python file in your python scripts directory:

  6. Open the project in VS Code by typing:.

  7. Open VS Code File Explorer by clicking CTRL + SHIFT + E Press (or in the menu to view > Explorer navigate) and select the file “list-directory-contents.py” that has just been created. The Microsoft Python extension automatically loads a Python interpreter. You can see which interpreter was loaded at the bottom of the VS Code window.

    Note

    Python is a translated language; that is, it acts as a virtual machine that emulates a physical computer. Different types of Python interpreters are available for you to use: Python 2, Python 3, Anaconda, PyPy, etc. To run Python code and get Python IntelliSense, you need to tell VS Code which interpreter to use. You should stick with the interpreter that VS Code chooses by default (Python 3 in our case) unless you have a specific reason to choose otherwise. To change the Python interpreter, select the interpreter currently displayed in the blue bar at the bottom of the VS Code window, or open the Command palette (CTRL + SHIFT + P) and enter the command Python: select interpreter a. This will bring up a list of the currently installed Python interpreters. Learn more about configuring Python environments.

  8. Paste the following code into the list-directory-contents.py file, then select to save out:

  9. Open the built-in VS Code terminal (CTRL + ` , use the grave accent), and enter the src directory where you just saved the python script:

  10. Run the script in PowerShell with:

    You should see output like the following:

  11. Use Python to output the filesystem directory to its own text file by entering this command directly in your PowerShell terminal:.

Congratulations! You have just written an automated system administration script that reads the created directories and files, uses Python to display the directory structure and then outputs it to a separate text file.

Note

If you cannot install Python 3 from the Microsoft Store, here is an example of how the path is processed for this sample script.

Sample script to change all files in a directory

This example uses the files and directories you just created. Each of the files is renamed by adding the date of the last change to the file name.

  1. Create in the folder src of python scripts-Directory a new python file for the script:

  2. Open the update-filenames.py file, paste and save the following code in the file:

    Note

    "Os.getmtime" returns a timestamp in ticks that is not easy to read. It must first be converted to a standard DateTime string.

  3. Test the update-filenames.py script by running it: and then run the list-directory-contents.py script again:.

  4. You should see output similar to the following:

  5. Use Python to output the new file system directory names, prefixed with the timestamp of the last change, to a separate text file by entering this command directly in your PowerShell terminal:.

By now, you must have seen some interesting aspects of using Python scripts to automate basic system administration tasks. There is a lot more you can learn about this, of course, but we hope this is a good place to start. Below are some additional, in-depth resources.

Additional resources

  • Python Docs: File and Directory Access: Python documentation for working with file systems and using modules to read the properties of files, change paths for portability, and create temporary files.
  • Learn Python: String_Formatting Tutorial: Learn more about using the% operator to format strings.
  • 10 Python File System Methods You Should Know: Advanced article on manipulating files and folders with and.
  • The Hitchhikers Guide to Python: Systems Administration: A "dogmatic guide" that provides overviews and best practices for Python. This section covers the tools and frameworks for system administration. This guide is hosted on GitHub so that you can raise problems and contribute.

Is this page helpful?