Matthieu Choplin
We create a file object with the following syntax:
file = open(filename, mode)
Mode | Description |
---|---|
r | Open a file for reading only |
r+ | Open a file for both reading and writing |
w | Open a file for writing only |
a | Open a file for appending data. Data are written to the end of the file |
rb | Open a file for reading binary data |
wb | Open a file for writing binary data |
Program that creates a file if it does not exist (an existing file with the same name will be erased) and write in it:
def main():
# Open file for output
outfile = open("Python_projects.txt", "w")
# Write data to the file
outfile.write("Django\n")
outfile.write("Flask\n")
outfile.write("Ansible")
outfile.close() # Close the output file
main() # Call the main function
import os.path
if os.path.isfile("Python_projects.txt"):
print("Python_projects.txt exists")
After a file is opened for reading data, you can use:
def main():
# Open file for input
infile = open("Python_projects.txt", "r")
print("Using read(): ")
print(infile.read())
infile.close() # Close the input file
main() # Call the main function
def main():
infile = open("Python_projects.txt", "r")
print("\nUsing read(number): ")
s1 = infile.read(4) # read till the 4th character
print(s1)
s2 = infile.read(10) # read from 4th till 4th+10th
print(repr(s2)) # a new line is also a character \n
infile.close() # Close the input file
main() # Call the main function
def main():
infile = open("Python_projects.txt", "r")
print("\nUsing readline(): ")
line1 = infile.readline()
line2 = infile.readline()
line3 = infile.readline()
line4 = infile.readline()
print(repr(line1))
print(repr(line2))
print(repr(line3))
print(repr(line4))
infile.close() # Close the input file
main() # Call the main function
def main():
# Open file for input
infile = open("Python_projects.txt", "r")
print("\n(4) Using readlines(): ")
print(infile.readlines()) # a list of lines
infile.close() # Close the input file
main() # Call the main function
You can use the 'a' mode to open a file for appending data to an existing file.
def main():
# Open file for appending data
outfile = open("Info.txt", "a")
outfile.write("\nPython is interpreted\n")
outfile.close() # Close the input file
main() # Call the main function
To write numbers, convert them into strings, and then use the write method to write them to a file. In order to read the numbers back correctly, you should separate the numbers with a whitespace character such as " " (empty string) or '\n' (new line).
from random import randint
def main():
# Open file for writing data
outfile = open("Numbers.txt", "w")
for i in range(10):
outfile.write(str(randint(0, 9)) + " ")
outfile.close() # Close the file
# Open file for reading data
infile = open("Numbers.txt", "r")
s = infile.read()
numbers = [int(x) for x in s.split()]
for number in numbers:
print(number, end = " ")
infile.close() # Close the file
main() # Call the main function
Write a program that prompts the user to enter a file and counts the number of occurrences of each letter in the file regardless of case.
Only take the characters of the alphabet, you can get them with the following
from string import ascii_lowercase
print(ascii_lowercase) # abcdefghijklmnopqrstuvwxyz
Solution
Hide solution
from string import ascii_lowercase
from pprint import pprint
def main():
filename = input("Enter a filename: ").strip()
dict_of_letter = {}
f = open(filename)
for line in f:
for letter in line.lower():
if letter in ascii_lowercase:
if letter in dict_of_letter:
dict_of_letter[letter] += 1
else:
dict_of_letter[letter] = 1
f.close()
pprint(dict_of_letter)
main()
Using Python, you can write simple code to read data from a Website. All you need to do is to open a URL link using the urlopen function as follows:
import urllib.request
infile = urllib.request.urlopen('http://example.org/')
html_page = infile.read().decode()
print(html_page)
It represents the full HTML of the page just as a web browser would see it
Solution
Hide solution
from pprint import pprint
from string import ascii_lowercase
import urllib.request
def main():
url = input("Enter an URL for a file: ").strip()
infile = urllib.request.urlopen(url)
f = infile.read().decode() # Read the content as string
dict_of_letter = {}
for line in f:
for letter in line.lower():
if letter in ascii_lowercase:
if letter in dict_of_letter:
dict_of_letter[letter] += 1
else:
dict_of_letter[letter] = 1
pprint(dict_of_letter)
main()
What happens if the user enters a file or an URL that does not exist? The program would be aborted and raises an error. For example, if you run count_letter.py with an incorrect input:
c:\session6\python count_letter.py
Enter a filename: non_existant_file.txt
Traceback (most recent call last):
File "path_to/count_letter.py", line 18, in <module>
main()
File "path_to/count_letter.py", line 7, in main
f = open(filename)
FileNotFoundError: [Errno 2]
No such file or directory: 'non_existant_file.txt'
Process finished with exit code 1
Catching one exception type
try:
<body>
except <ExceptionType>:
<handler>
Catching several exception types
try:
<body>
except <ExceptionType>:
<handler1>
<handler1>
...
except <ExceptionTypeN>:
<handlerN>
except:
<handlerExcept>
else:
<process_else> # will be executed if not exception
finally:
<process_finally> # executed with or without exception
def main():
try:
number1, number2 = int(
input("Enter two integers,"
"separated by a comma: "))
result = number1 / number2
print("Result is " + str(result))
except ZeroDivisionError:
print("Division by zero!")
except SyntaxError:
print("A comma may be missing in the input")
except:
print("Something wrong in the input")
else:
print("No exceptions")
finally:
print("The finally clause is executed")
main()
You learned how to write the code to handle exceptions in the preceding section. Where does an exception come from? How is an exception created? Exceptions are objects and objects are created from classes. An exception is raised from a function. When a function detects an error, it can create an object of an appropriate exception class and raise the object, using the following syntax:
raise ExceptionClass("Something is wrong")
You can access the exception object in the except clause with the as keyword.
try:
number = int(input("Enter a number: "))
print("The number entered is", number)
except NameError as ex:
print("Exception:", ex)
It is good practice to use the with keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally blocks:
with open('Python_projects.txt', 'r') as f:
read_data = f.read()
assert f.closed
import json
serialized_data = json.dumps(
['foo', {'bar': ('baz', None, 1.0, 2)}])
print(serialized_data)
deserialized_data = json.loads(serialized_data)
print(deserialized_data)
How to get the capital of each country?
import json
from urllib import request
infile = request.urlopen(
'https://restcountries.eu/rest/v1/all')
content_as_python_obj = json.loads(infile.read().decode())
for country in content_as_python_obj:
print(country['borders'])
Can you see what object is the "borders"?
import json
from urllib import request
infile = request.urlopen(
'https://restcountries.eu/rest/v1/all')
content_as_python_obj = json.loads(infile.read().decode())
for country in content_as_python_obj:
print(country['capital'])
from urllib import parse, request
import json
serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'
while True:
address = input('Enter location (q to quit): ')
if len(address) < 1 or address.lower() == 'q': # sentinel value, press q to quit
break
url = serviceurl + parse.urlencode({'sensor': 'false', 'address': address})
print('Retrieving', url)
uh = request.urlopen(url)
data = uh.read().decode('utf-8')
print('Retrieved', len(data), 'characters')
js = json.loads(data)
if 'status' not in js or js['status'] != 'OK':
print('==== Failure To Retrieve ====')
print(data)
continue
lat = js["results"][0]["geometry"]["location"]["lat"]
lng = js["results"][0]["geometry"]["location"]["lng"]
print('lat', lat, 'lng', lng)
location = js['results'][0]['formatted_address']
print(location)
import tweepy
consumer_key = 'get_your_own'
consumer_secret = 'get_your_own'
access_token = 'get_your_own'
access_secret = 'get_your_own'
def main():
auth = tweepy.auth.OAuthHandler(consumer_key,
consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
tweets = api.search(q='#python')
for t in tweets:
print(t.created_at, t.text, '\n')
main()