python:file_handling
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
python:file_handling [2016/10/18 10:35] – created peter | python:file_handling [2020/07/15 09:30] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 17: | Line 17: | ||
</ | </ | ||
- | Next, save your file and make sure you know where you put it. In our example, our user sammy, saved the file here: /users/sammy/days.txt. This will be very important in later steps, where we open the file in Python. | + | Next, save your file and make sure you know where you put it. In our example, our user john, saved the file here: /home/john/ |
Now that we have a txt file to process, we can begin our code! | Now that we have a txt file to process, we can begin our code! | ||
+ | |||
===== Opening a File ===== | ===== Opening a File ===== | ||
Line 25: | Line 26: | ||
Before we can write our program, we have to create a Python programming file, so create the file **files.py** with your text editor. | Before we can write our program, we have to create a Python programming file, so create the file **files.py** with your text editor. | ||
- | To open a file in Python, we first need some way to associate the file on disk with a variable in Python. | + | To open a file in Python, we first need some way to associate the file on disk with a variable in Python. |
<file python files.py> | <file python files.py> | ||
Line 31: | Line 32: | ||
</ | </ | ||
- | We will then use Python' | + | We will then use Python' |
- | ' | + | * ' |
- | ' | + | |
- | ' | + | |
- | ' | + | |
- | ' | + | |
- | In this example, we only want to read from the file, so we will use the ' | + | |
- | files.py | + | In this example, we only want to read from the file, so we will use the **' |
+ | |||
+ | <file python | ||
days_file = open(path,' | days_file = open(path,' | ||
+ | </ | ||
+ | |||
After we have opened the file, we can then read from it, which we will do in the next step. | After we have opened the file, we can then read from it, which we will do in the next step. | ||
- | Step 3 — Reading a File | ||
- | Since our file has been opened, we can now manipulate it (i.e. read from it) through the variable we assigned to it. Python provides three related operations for reading information from a file. We'll show how to use all three operations as examples that you can try out to get an understanding of how they work. | ||
- | The first operation < | + | ===== Reading |
+ | Since our file has been opened, we can now manipulate it (i.e. read from it) through the variable we assigned to it. Python provides three related operations for reading information from a file. We'll show how to use all three operations as examples that you can try out to get an understanding of how they work. | ||
+ | |||
+ | The first operation **< | ||
+ | |||
+ | <file python files.py> | ||
days_file.read() | days_file.read() | ||
- | Output | + | </ |
+ | |||
+ | Returns: | ||
+ | |||
+ | < | ||
' | ' | ||
- | The second operation | + | </code> |
+ | The second operation **< | ||
+ | |||
+ | <file python files.py> | ||
days_file.readline() | days_file.readline() | ||
- | Output | + | </ |
+ | |||
+ | Returns: | ||
+ | |||
+ | < | ||
' | ' | ||
- | Therefore, once you read a line with the readline operation it will pass to the next line. So if you were to call this operation again, it would return the next line in the file, as shown. | + | </ |
+ | Therefore, once you read a line with the readline operation it will pass to the next line. So if you were to call this operation again, it would return the next line in the file, as shown. | ||
+ | |||
+ | <file python files.py> | ||
days_file.readline() | days_file.readline() | ||
- | Output | + | </ |
+ | |||
+ | Returns: | ||
+ | |||
+ | < | ||
' | ' | ||
- | The last operation, | + | </code> |
+ | |||
+ | The last operation, **< | ||
+ | |||
+ | <file python files.py> | ||
days_file.readlines() | days_file.readlines() | ||
- | Output | + | </ |
+ | |||
+ | Returns: | ||
+ | |||
+ | < | ||
[' | [' | ||
- | Something to keep in mind when you are reading from files, once a file has been read using one of the read operations, it cannot be read again. For example, if you were to first run days_file.read() followed by days_file.readlines() the second operation would return an empty string. Therefore, anytime you wish to read from a file you will have to first open a new file variable. Now that we have read from a file, let's learn how to write to a new file. | + | </ |
- | Step 4 — Writing a File | + | Something |
- | In this step, we are going to write a new file that includes the title Days of the Week followed by the days of the week. First, let' | + | |
- | files.py | + | |
+ | ===== Writing a File ===== | ||
+ | |||
+ | In this step, we are going to write a new file that includes the title **Days of the Week** followed by the days of the week. First, let's create our title variable. | ||
+ | |||
+ | <file python | ||
title = 'Days of the Week\n' | title = 'Days of the Week\n' | ||
- | We also need to store the days of the week in a string variable, which we'll call days. To make it easier to follow, we include the code from the steps above. We open the file in read mode, read the file, and store the returned output from the read operation in our new variable days. | + | </file> |
- | files.py | + | We also need to store the days of the week in a string variable, which we'll call **days**. |
- | path = '/users/sammy/ | + | |
+ | <file python | ||
+ | path = '/home/john/ | ||
days_file = open(path,' | days_file = open(path,' | ||
days = days_file.read() | days = days_file.read() | ||
- | Now that we have variables for title and days of the week, we can begin writing to our new file. First, we need to specify the location of the file. Again, we will use the directory / | + | </file> |
- | files.py | + | Now that we have variables for title and days of the week, we can begin writing to our new file. First, we need to specify the location of the file. Again, we will use the directory / |
- | new_path = '/users/sammy/ | + | |
+ | <file python | ||
+ | new_path = '/home/john/ | ||
new_days = open(new_path,' | new_days = open(new_path,' | ||
- | Important to note, if new_days.txt already existed before opening the file its old contents would have been destroyed, so be careful when using the ' | + | </file> |
- | Once our new file is opened, we can put data into the file, using the write operation, < | + | Important to note, if new_days.txt already existed before opening |
- | files.py | + | Once our new file is opened, we can put data into the file, using the write operation, **< |
+ | |||
+ | <file python | ||
new_days.write(title) | new_days.write(title) | ||
print(title) | print(title) | ||
Line 95: | Line 138: | ||
new_days.write(days) | new_days.write(days) | ||
print(days) | print(days) | ||
- | Lastly, whenever we are finished with a file, we need to make sure to close it. We show this in our final step. | + | </file> |
- | Step 5 — Closing a File | + | Lastly, whenever we are finished with a file, we need to make sure to close it. We show this in our final step. |
- | Closing | + | |
- | files.py | + | |
+ | ===== Closing a File ===== | ||
+ | |||
+ | Closing a file makes sure that the connection between the file on disk and the file variable is finished. | ||
+ | |||
+ | <file python | ||
days_file.close() | days_file.close() | ||
new_days.close() | new_days.close() | ||
+ | </ | ||
+ | |||
We're now finished processing files in Python and can move on to looking over our code. | We're now finished processing files in Python and can move on to looking over our code. | ||
- | Step 6 — Checking our Code | ||
- | Before we run our code, let's make sure everything looks good. The final product should look something like this: | ||
- | files.py | + | ===== Checking our Code ===== |
- | path = '/users/sammy/ | + | |
+ | Before we run our code, let's make sure everything looks good. The final product should look something like this: | ||
+ | |||
+ | <file python | ||
+ | path = '/home/john/ | ||
days_file = open(path,' | days_file = open(path,' | ||
days = days_file.read() | days = days_file.read() | ||
- | + | new_path = '/home/john/ | |
- | new_path = '/users/sammy/ | + | |
new_days = open(new_path,' | new_days = open(new_path,' | ||
Line 126: | Line 176: | ||
days_file.close() | days_file.close() | ||
new_days.close() | new_days.close() | ||
+ | </ | ||
+ | |||
After saving your code, open up terminal and run your Python script, like so: | After saving your code, open up terminal and run your Python script, like so: | ||
+ | <code bash> | ||
python files.py | python files.py | ||
+ | </ | ||
+ | |||
Our output should look like this: | Our output should look like this: | ||
- | Output | + | < |
Days of the Week | Days of the Week | ||
Line 141: | Line 196: | ||
Saturday | Saturday | ||
Sunday | Sunday | ||
- | Now, let's double check our code fully worked by opening our new file (new_days.txt). If all went well, when we open our new file, it should look like this: | + | </code> |
- | new_days.txt | + | Now, let's double check our code fully worked by opening our new file (new_days.txt). If all went well, when we open our new file, it should look like this: |
+ | |||
+ | <file text new_days.txt> | ||
Days of the Week | Days of the Week | ||
Monday | Monday | ||
Line 152: | Line 209: | ||
Saturday | Saturday | ||
Sunday | Sunday | ||
+ | </ | ||
+ | |||
Your file will look the same or similarly — you have successfully completed this tutorial! | Your file will look the same or similarly — you have successfully completed this tutorial! | ||
+ |
python/file_handling.1476786909.txt.gz · Last modified: 2020/07/15 09:30 (external edit)