Advanced
Files and Directories
Master file input/output and directory management using Python’s standard libraries for reading, writing, and navigating the filesystem.
Working with Files and Directories
Reading and Writing Files
Use open()
with modes 'r'
(read), 'w'
(write), and 'a'
(append):
pythonCopyEdit# Writing to a file with open("example.txt", "w") as file: file.write("Hello, world!\n") # Reading from a file with open("example.txt", "r") as file: content = file.read() print(content)
Using with
ensures files are properly closed.
Working with Directories
The os
and pathlib
modules help interact with directories.
pythonCopyEditimport os print(os.getcwd()) # Current working directory # Create a new directory os.mkdir("new_folder") # List files in directory print(os.listdir(".")) # Using pathlib from pathlib import Path path = Path("new_folder") print(path.exists())
Reading and Writing CSV Files
pythonCopyEditimport csv # Writing CSV with open("data.csv", "w", newline="") as csvfile: writer = csv.writer(csvfile) writer.writerow(["Name", "Age"]) writer.writerow(["Alice", 30]) # Reading CSV with open("data.csv", "r") as csvfile: reader = csv.reader(csvfile) for row in reader: print(row)
Working with Files and Directories
Reading and Writing Files
Use open()
with modes 'r'
(read), 'w'
(write), and 'a'
(append):
pythonCopyEdit# Writing to a file with open("example.txt", "w") as file: file.write("Hello, world!\n") # Reading from a file with open("example.txt", "r") as file: content = file.read() print(content)
Using with
ensures files are properly closed.
Working with Directories
The os
and pathlib
modules help interact with directories.
pythonCopyEditimport os print(os.getcwd()) # Current working directory # Create a new directory os.mkdir("new_folder") # List files in directory print(os.listdir(".")) # Using pathlib from pathlib import Path path = Path("new_folder") print(path.exists())
Reading and Writing CSV Files
pythonCopyEditimport csv # Writing CSV with open("data.csv", "w", newline="") as csvfile: writer = csv.writer(csvfile) writer.writerow(["Name", "Age"]) writer.writerow(["Alice", 30]) # Reading CSV with open("data.csv", "r") as csvfile: reader = csv.reader(csvfile) for row in reader: print(row)
Working with Files and Directories
Reading and Writing Files
Use open()
with modes 'r'
(read), 'w'
(write), and 'a'
(append):
pythonCopyEdit# Writing to a file with open("example.txt", "w") as file: file.write("Hello, world!\n") # Reading from a file with open("example.txt", "r") as file: content = file.read() print(content)
Using with
ensures files are properly closed.
Working with Directories
The os
and pathlib
modules help interact with directories.
pythonCopyEditimport os print(os.getcwd()) # Current working directory # Create a new directory os.mkdir("new_folder") # List files in directory print(os.listdir(".")) # Using pathlib from pathlib import Path path = Path("new_folder") print(path.exists())
Reading and Writing CSV Files
pythonCopyEditimport csv # Writing CSV with open("data.csv", "w", newline="") as csvfile: writer = csv.writer(csvfile) writer.writerow(["Name", "Age"]) writer.writerow(["Alice", 30]) # Reading CSV with open("data.csv", "r") as csvfile: reader = csv.reader(csvfile) for row in reader: print(row)