Skip to content

Commit

Permalink
Update writing to a file.md
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavmodx authored Dec 28, 2018
1 parent 86c6767 commit 25bc739
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions Learn to handle your file/writing to a file.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
**Authors:** Annu Jolie

**Edits:**
**Edits:** Pranav Shridhar

***


## A simple file program

Now let us try to write a simple python program to add text into an empty file
Now let us try to write a simple python program to add text into an empty file.
<img src="http://eras4solutions.com/wp-content/uploads/2018/03/612948-BOS0148.gif" width="150" height="150" />
```python
new_file = open("test.txt", "w")
new_file.write("This is my first python program \n I am really excited about it!!)
new_file.close()
```

Let us try to break it down and understand each line of code in the above program
Let us try to break it down and understand each line of code in the above program.
### Line 1-
```diff
+ Open() function
Expand All @@ -28,15 +28,15 @@ On the first line, we have the open function.We declared a variable *new_line* t

2. **Mode**: Here we used "w" letter in our argument, which indicates write mode.This arguement tells python whether we are reading from a file or writing into it. Other than "w", we have different other modes for opening a file

*Different methods (modes) for opening a file:*
*Different methods (modes) for opening a file :*

* "r" - Read - Default value. Opens a file for reading, error if the file does not exist
* "a" - Append - Opens a file for appending, creates the file if it does not exist
* "w" - Write - Opens a file for writing, creates the file if it does not exist
* "r" - Read : Default value. Opens a file for reading, error if the file does not exist.
* "a" - Append : Opens a file for appending, creates the file if it does not exist.
* "w" - Write : Opens a file for writing, creates the file if it does not exist.

In addition you can specify if the file should be handled as binary or text mode
* "t" - Text - Default value. Text mode
* "b" - Binary - Binary mode (e.g. images)
* "t" - Text : Default value. Text mode
* "b" - Binary : Binary mode (e.g. images)
_____________________
### Line 2-
```diff
Expand Down Expand Up @@ -64,7 +64,7 @@ resources taken up by the open file.Python automatically closes a file when the
______________________________


We have successfully created a new file and entered some contents to it. Let us see one more example:-
We have successfully created a new file and entered some contents to it. Let us see one more example :-

#### *Example II*

Expand All @@ -77,7 +77,7 @@ for i in range(5):
f.close()

```
The result after code execution is as follows:-
The result after code execution is as follows :-
```python
This is line 1
This is line 2
Expand Down

0 comments on commit 25bc739

Please sign in to comment.