0% found this document useful (0 votes)
397 views2 pages

Python OS Interaction Guide

This document provides an overview of using Python to interact with the operating system. It covers reading and writing files, working with directories and paths, reading and writing CSV files, standard streams, environment variables, passing arguments, exit statuses, and running system commands from Python using the subprocess module. Key topics include opening and reading files, changing directories, listing directory contents, getting file metadata like size and last modified time, and running OS commands to get the date.

Uploaded by

Enrique Cheung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
397 views2 pages

Python OS Interaction Guide

This document provides an overview of using Python to interact with the operating system. It covers reading and writing files, working with directories and paths, reading and writing CSV files, standard streams, environment variables, passing arguments, exit statuses, and running system commands from Python using the subprocess module. Key topics include opening and reading files, changing directories, listing directory contents, getting file metadata like size and last modified time, and running OS commands to get the date.

Uploaded by

Enrique Cheung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Using Python to interact with the Operating System

Google IT Automation with Python, Coursera

Reading Files
[Link]

file = open(“[Link]”)
print([Link]()) read single line
[Link]() read whole file
[Link]()

with open(“[Link]”) as file: automatically close the file


print([Link]())

with open(“[Link]) as file:


for line in file:
print(line)

file = open(“[Link]”)
print([Link]()) returns a list containing all the lines
[Link]()

with open(“[Link]”, “w”) as file: automatically close the file


[Link](“It was a dark”)

open “r” read, “w” write, “a” append, “r+” read, write

Working with file & directory


Import os
[Link](“[Link]”)
[Link](“[Link]”, “[Link]”)
[Link](“[Link]”) exist file
[Link](“[Link]”)
[Link](“[Link]”) time created

Import datetime
Timestamp = [Link](“[Link]”)
[Link](“[Link]”) full path
[Link]() current dir
[Link](“dir”)
[Link](“dir”) change dir
[Link](“dir”)
[Link](“dir”) list file in dir
[Link]

CSV files
Import csv
F = open(“[Link]”)
Csv_f=[Link](f)
For row in csv_f:
Name, phone, role = row same amount of variable in left side
Print(“{} {}”.format(name, phone)
[Link]()

Generating csv
Hosts = [[“fsdjsajf”, “djfdj”],[“djfdj”, “asfdsf”]]
With open(‘[Link]’, ‘w’) as host_csv:
Writer = [Link](host_csv)
[Link](hosts)

Reading and writing csv with Dictionary


With open(‘[Link]’, ‘w’) as host_csv:
reader = [Link](host_csv)
for row in reader:
print({} {}).format(row[“name”], row[“user”])

csv dictionary
name,version,status,users
Mail,4.35,production,345

Week 4
Standard stream. Stdin, stdout, stderr
Environmental Variable
[Link](“HOME”, “”) no error if there is no key
Export variable=value assign variable

Passing argument
[Link] one two three

Import sys
Print([Link]) print one two three(separate element list)

Exit status
Echo $? From shell. 0 no error
Python script [Link](number) whatever number we want

Running system commands in Python


Sub processes
[Link](["date"])

You might also like