Browse Source

stream -> file

master
Dimitri Merejkowsky 6 years ago
parent
commit
37a33f8494
3 changed files with 8 additions and 8 deletions
  1. +3
    -3
      sessions/python-02.tex
  2. +2
    -2
      sources/05-airports.py
  3. +3
    -3
      sources/06-top-words.py

+ 3
- 3
sessions/python-02.tex View File

@@ -641,9 +641,9 @@ Mais il vaut mieux recopier le code vous-mêmes.


\begin{lstlisting} \begin{lstlisting}
def read_words(): def read_words():
stream = open("pendu.txt")
contents = stream.read()
stream.close()
file = open("pendu.txt")
contents = file.read()
file.close()
words = splitlines() words = splitlines()
return words return words
\end{lstlisting} \end{lstlisting}


+ 2
- 2
sources/05-airports.py View File

@@ -3,8 +3,8 @@ import sys


def build_airport_dict(): def build_airport_dict():
result = dict() result = dict()
stream = open("airports.txt", "r")
contents = stream.read()
file = open("airports.txt", "r")
contents = file.read()
lines = contents.splitlines() lines = contents.splitlines()
for line in lines: for line in lines:
words = line.split(" ", maxsplit=1) words = line.split(" ", maxsplit=1)


+ 3
- 3
sources/06-top-words.py View File

@@ -17,11 +17,11 @@ def get_word(chunk):


def main(): def main():
filename = sys.argv[1] filename = sys.argv[1]
stream = open(filename, "r")
file = open(filename, "r")


scores = {} scores = {}


for line in stream.readlines():
for line in file.readlines():
for chunk in line.split(): for chunk in line.split():
word = get_word(chunk) word = get_word(chunk)
if word: if word:
@@ -30,7 +30,7 @@ def main():
else: else:
scores[word] += 1 scores[word] += 1


stream.close()
file.close()




main() main()