Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/file #159

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions docs/file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
File handling
=============

A file is some information or data which stays in the computer storage devices. You already know about different kinds of file , like your music files, video files, text files. Python gives you easy ways to manipulate these files. Generally we divide files in two categories, text file and binary file. Text files are simple text where as the binary files contain binary data which is only readable by computer.
A file is some information or data which stays in the computer storage devices. You already know about different kinds of file, like your music files, video files, text files. Python gives you easy ways to manipulate these files. Generally we divide files in two categories, text file and binary file. Text files are simple text where as the binary files contain binary data which is only readable by computer.

File opening
============

To open a file we use *open()* function. It requires two arguments, first the file path or file name, second which mode it should open. Modes are like
To open a file we use *open()* function. It requires two arguments, first the file path or file name, second which mode it should open. Modes are like:

+ "r" -> open read only, you can read the file but can not edit / delete anything inside
+ "r" -> open read only, you can read the file but can not edit / delete anything inside.

+ "w" -> open with write power, means if the file exists then delete all content and open it to write
+ "w" -> open with write power, means if the file exists then delete all content and open it to write.

+ "a" -> open in append mode
+ "a" -> open in append mode.

The default mode is read only, ie if you do not provide any mode it will open the file as read only. Let us open a file
The default mode is read only, i.e. if you do not provide any mode it will open the file as read only. Let us open a file:

::

Expand Down Expand Up @@ -98,7 +98,7 @@ Let us write a program which will take the file name as the input from the user

In the last line you can see that we closed the file object with the help of close() method.

The output
The output:

::

Expand All @@ -116,7 +116,7 @@ In real life scenarios we should try to use `with` statement. It will take care

>>> with open('setup.py') as fobj:
... for line in fobj:
... print line,
... print(line, end="")
...
#!/usr/bin/env python3
"""Factorial project"""
Expand Down Expand Up @@ -152,7 +152,7 @@ We can also pass the file object to the print function call, so that it writes i
>>> print("This is the last line.", file=fobj)
>>> fobj.close()

Now read the file we just created
Now read the file we just created.

::

Expand Down Expand Up @@ -198,7 +198,7 @@ The first value in *sys.argv* is the name of the command itself.
for i, x in enumerate(sys.argv):
print(i, x)

The output
The output:

::

Expand Down Expand Up @@ -231,7 +231,7 @@ Let us try to write an application which will count the spaces, tabs, and lines

:arg path: Path of the text file to parse

:return: A tuple with count of spacaes, tabs and lines.
:return: A tuple with count of spaces, tabs and lines.
"""
fd = open(path)
i = 0
Expand Down Expand Up @@ -268,14 +268,14 @@ Let us try to write an application which will count the spaces, tabs, and lines
sys.exit(-1)
sys.exit(0)

You can see that we have two functions in the program , *main* and *parse_file* where the second one actually parses the file and returns the result and we print the result in *main* function. By splitting up the code in smaller units (functions) helps us to organize the codebase and also it will be easier to write test cases for the functions.
You can see that we have two functions in the program, *main* and *parse_file* where the second one actually parses the file and returns the result and we print the result in *main* function. By splitting up the code in smaller units (functions) helps us to organize the codebase and also it will be easier to write test cases for the functions.



Let us write some real code
===========================

Do you know how many CPU(s) are there in your processor? or what is the model name?
Do you know how many CPU(s) are there in your processor? Or what is the model name?
Let us write some code which can help us to know these things.

If you are in Linux, then you can actually view the output of the *lscpu* command first.
Expand Down