Python File Operations

To kick things off: creating files. To do this, simply use the `open()` function with the ‘w’ mode to write data into a new file. Here’s an example script that creates a file called “my_new_file.txt” and writes some text inside it:

# Define the filename variable as a string
filename = "my_new_file.txt"

# Open the file with the given filename in write mode and assign it to the variable f
with open(filename, 'w') as f:
    # Write the string "Hello, world!" followed by a new line character to the file
    f.write("Hello, world!\n")

# Print a formatted string indicating that the file was created successfully
print(f"File {filename} created successfully.")

Now let’s move on to reading files. This is where things get really exciting (or at least mildly interesting). To read a file in Python, you can use the `open()` function with the ‘r’ mode and then loop through each line using a for-loop or list comprehension. Here’s an example script that reads from a file called “my_existing_file.txt” and prints out its contents:

# Define the name of the file to be read
filename = "my_existing_file.txt"

# Open the file in read mode and assign it to the variable 'f'
with open(filename, 'r') as f:
    # Loop through each line in the file
    for line in f:
        # Print out the contents of each line
        print(line)

# The 'with' statement automatically closes the file after the loop is finished
# This ensures that the file is properly closed and resources are freed up

But what if you want to append text to an existing file instead of creating a new one? No problem! Just use the `open()` function with the ‘a’ mode. Here’s an example script that appends some text to a file called “my_existing_file.txt”:

# Define the file name to be used
filename = "my_existing_file.txt"

# Open the file in 'append' mode, which allows for adding text to the end of the file
# Use the 'with' statement to automatically close the file after use
with open(filename, 'a') as f:
    # Use the 'write()' function to add the desired text to the file
    f.write("This is some additional text.\n")

# Print a message to confirm that the text was successfully appended to the file
print(f"Text appended to file {filename} successfully.")

Finally, renaming files using Python’s `os` module. To do this, simply use the `rename()` function and pass in the old filename and new filename as arguments:

# Import the os module to access file management functions
import os

# Define the old filename as a string
old_filename = "my_existing_file.txt"

# Define the new filename as a string
new_filename = "renamed_file.txt"

# Use the os.rename() function to rename the file, passing in the old and new filenames as arguments
os.rename(old_filename, new_filename)

# Print a success message using f-strings to include the filenames in the output
print(f"File {old_filename} renamed to {new_filename} successfully.")

And that’s it! You now have a basic understanding of Python file operations. Remember: always close your files after you’re done with them (unless you want to accidentally delete important data).

SICORPS