WSQ09 – Files

--Originally published at Program

So for this assignment I would like to see you create a function that receives as parameter the name of a file (this would be a string value like data.txt) and your function counts the number of lines and the number of characters in the file which it returns as a single value (but with two values). You will want to look at how to use and return a tuple from a function and how to open and read text files line by line.

?

Photo credit:

Files flickr photo by T a k shared under a Creative Commons (BY-NC) license


# WSQ09 – Multipart Data and Files

--Originally published at マルコ

Your Assignment

So for this assignment I would like to see you create a function that receives as parameter the name of a file (this would be a string value like data.txt) and your function counts the number of lines and the number of characters in the file which it returns as a single value (but with two values). You will want to look at how to use and return a tuple from a function and how to open and read text files line by line.

files

files2

Featured image:

http://www.pixiv.net/member_illust.php?mode=medium&illust_id=25862501

 


WSQ-09

--Originally published at Héctor Santillán

Se debe crear una función que reciba el nombre de un archivo, lo abra y cuente sus líneas y carácteres.

Iniciamos creando una función que tome el nombre ingresado y que aparte de abrir el archivo, lea las lineas y caracteres que contenga, valores que asignaremos a la tupla que se crea al final de la función.

Al final el programa sólo pedirá el nombre del archivo y desplegará el número de caracteres y líneas en el archivo abierto.

Este programa lo realizé con mis conocimientos previos y el apoyo de mis compañeros de clase.


Files!

--Originally published at Python learning

The problem for this week consisted of defining a function that opens a .txt file and counts the number of lines and characters in it ? .

To do it, I first researched about how to read and write files in python, and I found this very useful page:

https://www.digitalocean.com/community/tutorials/how-to-handle-plain-text-files-in-python-3

I then proceeded to start writing my code, but when I was almost done I faced a small problem, which was that it was counting the enters between lines as characters. I remembered that my teacher Ken had told us the day before about a function that eliminates additional spaces, but I couldn’t recall its name. I asked a friend from that class about it, and he reminded me of it: it is .strip()

I used said function, and lastly my program worked as it should ❤ :files

The text file I used was this:

texto

And the program ran like this:files_run


WSQ09 – Files

--Originally published at Elu's Blog

For this assignment these were my instructions:

So for this assignment I would like to see you create a function that receives as parameter the name of a file (this would be a string value like data.txt) and your function counts the number of lines and the number of characters in the file which it returns as a single value (but with two values). You will want to look at how to use and return a tuple from a function and how to open and read text files line by line.

This is what I came up with:

Captura de pantalla 2017-03-06 a la(s) 10.39.34.png

Captura de pantalla 2017-03-06 a la(s) 10.31.38.png

Captura de pantalla 2017-03-06 a la(s) 10.39.48.png

Reading and writing of text files

When using external files in python, there are a few things that can be done to it: read, write, append and the binary versions of the previous three.

To use a file, you have to first  open it with the open() function. open(‘[fileaddress]’,'[opening mode]’ <- this is how to use the function. One example is:

f = open(‘/Users/rafaelelu/Desktop/Tec/Primer Semestre/Fundamentos de Programación (Ken Bauer)/46 Simple Python Exercises/’text.txt’,’r’) <- This will open the ‘text.txt’ file in read mode.

‘r’ = read mode ‘rb’ = read in binary mode

‘w’ = write mode   ‘wb’ = write in binary mode

‘a’ = append mode   ‘ab’ = append in binary mode

Note: when opening a file in write mode, all of its text will be deleted previous to writing on it. Also if you open a file (in write mode) that doesn’t exist, it will create a file with that name in the same address as the programmed.

In my code above, I used ‘txt = f.read()’ in line 4. This assigns all characters of the file in the txt variable. To write in a file, use the .write() function and inside the parenthesis write

Continue reading "WSQ09 – Files"