Skip to content

Latest commit

 

History

History
77 lines (57 loc) · 2.17 KB

File metadata and controls

77 lines (57 loc) · 2.17 KB

Python Language Overview

File Management

Use Python to read and write file contents, as well as to create and delete files.

Reference:

See also: the csv module for reading and writing CSV files, and the os module for command-line-style file operations and functionality to help specify file paths.

To setup these examples, create a new directory on your Desktop called "file-mgmt" and navigate there from your command line. Create a Python script in that directory called my_script.py and place inside it contents from each of the following sections, respectively.

Writing Files

Write content to file:

file_name = "my_message.txt" # refers to a file path relative to the path from which you invoke your your script.

with open(file_name, "w") as file: # NOTE: "w" means "open the file for writing"
    file.write("Hello World")
    file.write("\n")
    file.write("\n")
    file.write("...")
    file.write("\n")
    file.write("\n")
    file.write("Goodbye")

#> Hello World
#>
#> ...
#>
#> Goodbye

Reading Files

Read file contents:

file_name = "my_message.txt" # refers to a file path relative to the path from which you invoke your your script.

with open(file_name, "r") as file: # NOTE: "r" means "open the file for reading"
    contents = file.read()
    print(contents)

#> Hello World.
#>
#> ...
#>
#> Goodbye World.

Split file contents on line breaks and loop through each line:

file_name = "my_message.txt" # refers to a file path relative to the path from which you invoke your your script.

with open(file_name, "r") as file: # NOTE: "r" means "open the file for reading"
    contents = file.read()
    lines = contents.split("\n")
    print("THERE ARE", len(lines), "LINES IN THIS FILE")
    for line in lines:
        print("LINE:", line)

#> THERE ARE 5 LINES IN THIS FILE
#> LINE: Hello World
#> LINE:
#> LINE: ...
#> LINE:
#> LINE: Goodbye.