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.
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.
To install Python using the Microsoft Store:
Go to the menu begin (lower left Windows icon), enter “Microsoft Store” and select the link to open the store.
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.
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.
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
Open the VS Code Extensions window by clicking CTRL + SHIFT + X Press (or navigate in the menu to view > Extensions).
Enter in the field above Search for extensions in the Marketplace The following: python.
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.
Open the Terminal in VS Code, select Show > terminal or alternatively use the shortcut CTRL + ` (with the grave accent).
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.
Open Python in your VS Code terminal by typing:.
Try the Python interpreter by typing:. Python returns your "Hello World" statement.
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.
Download Git for Windows from the git-scm website and install Git.
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.
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.
Open PowerShell from the menu begin (lower left Windows symbol).
Create a directory for the project: and then open the directory:.
Create some directories to use with our example script:
Create some files in these directories for use with our script:
Create a new python file in your python scripts directory:
Open the project in VS Code by typing:.
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.
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.
Paste the following code into the list-directory-contents.py file, then select to save out:
Open the built-in VS Code terminal (CTRL + ` , use the grave accent), and enter the src directory where you just saved the python script:
Run the script in PowerShell with:
You should see output like the following:
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.
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.
Create in the folder src of python scripts-Directory a new python file for the script:
Open the update-filenames.py file, paste and save the following code in the file:
"Os.getmtime" returns a timestamp in ticks that is not easy to read. It must first be converted to a standard DateTime string.
Test the update-filenames.py script by running it: and then run the list-directory-contents.py script again:.
You should see output similar to the following:
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.
- 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?
- Which humeral process articulates with the radius
- The Mini Cooper makes knocking noises when accelerating
- How to set up the Bose remote model rc48s2-27
- DIY How To Make A Reflective A Sketch
- Who makes cobalt cabinets 0054782
- Who is born today
- How do you type in the diameter
- Wholesale machine embroidery baby blanks
- Door WhatsApp Contacten Kwijt
- Matt Howard Mojave Water Agency
- Drive from Singapore to Malacca how long
- Whoever denied it delivered it as a comeback child
- LED light bulbs wholesale UK clothing
- Oh brother, where do you quote gopher
- How to hang the hose holder on the siding
- What does the standard setting of the employees mean
- How to prevent sleep while driving
- Toes that shift when you swim in the pools
- How to edit h files arduino nano
- Performing Arts Dance Theater Howell Michigan
- What does tree trunk mean
- Howard marks Oaktree Net Worth
- Officially licensed Greek products wholesale
- Orends are enough who subs what super