1
00:00:00,380 --> 00:00:07,500
In this video, you learn how to create new text files via Python and how to add content to those text

2
00:00:07,500 --> 00:00:10,290
files to create new tax files.

3
00:00:10,500 --> 00:00:13,890
We use the open function as a built in function.

4
00:00:14,040 --> 00:00:15,690
So you see, I didn't import anything.

5
00:00:15,960 --> 00:00:20,940
The open function gets a file path as inputs and also the modes.

6
00:00:21,030 --> 00:00:24,090
These are the two most important arguments.

7
00:00:24,360 --> 00:00:33,360
The file path could be just a file name, such as File 1.60, so a new file with this name will be created

8
00:00:33,630 --> 00:00:39,390
in the working directory of the working directory is the directory where the Python file is located.

9
00:00:39,750 --> 00:00:46,010
So a new File 1.60 will be created in the same directory with mean that supplied event.

10
00:00:46,050 --> 00:00:55,320
The next argument is the moods of the modes could be either W or are for non-binary files for text files.

11
00:00:56,280 --> 00:00:58,170
So in our case, it is W.

12
00:00:58,350 --> 00:01:08,790
If you were processing binary files such as PDA files, jpg files, etc. you'd use WB, but we are working

13
00:01:08,790 --> 00:01:09,570
on takes files.

14
00:01:10,050 --> 00:01:16,920
See us with files, estimable files and all the files that have text in them and text only.

15
00:01:17,220 --> 00:01:22,890
So w know of this function returns a file object.

16
00:01:22,890 --> 00:01:30,210
So a specific python file type, a file object and you need to store all of that file object in a variable.

17
00:01:30,420 --> 00:01:35,220
So let's create a variable called file so that we'll return a file object.

18
00:01:35,670 --> 00:01:43,680
In other words, we have created the file inside Python, and we can use the right methods to write

19
00:01:43,680 --> 00:01:44,910
inside that file.

20
00:01:45,300 --> 00:01:46,690
What do we rights?

21
00:01:46,710 --> 00:01:50,670
Well, text and text is represented by strings in Python.

22
00:01:51,000 --> 00:01:57,630
So let's say first the text you wrote that and you can write all the lines if you like.

23
00:01:57,630 --> 00:02:00,420
You can apply that method as many times as you want.

24
00:02:01,600 --> 00:02:03,360
Second text and so on.

25
00:02:04,020 --> 00:02:11,640
Once you are done writing the files, you want to close the file with file that's close and then run,

26
00:02:12,840 --> 00:02:18,030
and you see that file one that was generated in my working directory.

27
00:02:18,300 --> 00:02:22,350
And this is the first string that we added and this is a second string.

28
00:02:23,460 --> 00:02:29,310
So not very impressive, perhaps because you were expecting this to be something like this, I believe.

29
00:02:29,520 --> 00:02:38,400
Well, that didn't happen because this break line is also a character, and we didn't provide that character.

30
00:02:38,670 --> 00:02:41,450
So we just said, right, that text.

31
00:02:41,460 --> 00:02:44,070
And then just after that, right, the other text.

32
00:02:44,610 --> 00:02:47,220
So Python doesn't know that she wants a brick line.

33
00:02:47,220 --> 00:02:51,060
If you want a brick line, you want to add backslash.

34
00:02:51,360 --> 00:02:57,750
And that means after the first text, there will be a brick line and then comes to whatever comes after.

35
00:02:58,620 --> 00:02:59,960
So we run again.

36
00:02:59,970 --> 00:03:08,070
Now, if I run this, this file name will override the existing file name if there is something.

37
00:03:08,700 --> 00:03:14,160
In other words, if this file path exists, it will be overwritten.

38
00:03:14,550 --> 00:03:20,850
So run this and you see that now we do have this break line.

39
00:03:24,030 --> 00:03:28,290
If you want to break lines, you can add another backslash and run.

40
00:03:30,510 --> 00:03:41,950
And now we see this blank line in here, another way to write a book of codes is to just delete this

41
00:03:42,180 --> 00:03:44,970
right methods and do.

42
00:03:46,900 --> 00:03:54,460
Contends Eagles to a multi-line string, so with multi-line strings, you have this strip of quotes

43
00:03:54,460 --> 00:04:02,920
in here and then triple records in here and in sides here you can write texts such as first text and

44
00:04:02,920 --> 00:04:05,020
then second text.

45
00:04:05,260 --> 00:04:12,470
Now you don't need to answer backslash and here, because in multi-line strings, the break line with

46
00:04:12,610 --> 00:04:14,140
its year is recognized.

47
00:04:14,150 --> 00:04:21,760
So for example, let's have this or maybe two lengthwise and then apply file.

48
00:04:21,790 --> 00:04:25,450
That's right, and we write that string content.

49
00:04:26,920 --> 00:04:30,190
So this time we just passed the variable directly run.

50
00:04:31,580 --> 00:04:35,330
Check and we have one to empty lines.

51
00:04:35,840 --> 00:04:40,400
However, notice that we also have a brake line here and also a brake line here.

52
00:04:40,610 --> 00:04:48,740
So if you don't warn them you want to avoid the first brick line, so you want to delete that and start

53
00:04:48,740 --> 00:04:50,360
directly after the quotes.

54
00:04:50,540 --> 00:04:51,290
And also here.

55
00:04:52,830 --> 00:04:56,220
Put the quotes directly after the lost tax run again.

56
00:04:57,240 --> 00:05:02,970
And yeah, this is the puts no brake line in the first line this time.

57
00:05:04,820 --> 00:05:07,820
No notes is the file that's closed methods.

58
00:05:08,300 --> 00:05:17,820
This is a method that will close the file for operations and you cannot write and it takes off of that.

59
00:05:17,820 --> 00:05:21,890
So let's say third tax year if we run this.

60
00:05:23,130 --> 00:05:30,690
We're going to get an error it cease operation on close file, so this method basically controls the

61
00:05:30,690 --> 00:05:33,480
flow of operating with files.

62
00:05:34,050 --> 00:05:42,960
Now there is a better method to do the writing of a file, the creation of a file that is by using a

63
00:05:42,960 --> 00:05:50,190
with context manager with works like this you see with.

64
00:05:51,890 --> 00:05:57,320
Open function, so you could vet as file.

65
00:05:58,220 --> 00:06:04,010
So this is a new way to bind this variable with this.

66
00:06:05,420 --> 00:06:14,420
With the output of this function, so we do need that now in science, here you apply, find those rights

67
00:06:14,420 --> 00:06:16,250
and your rights wherever you like.

68
00:06:17,360 --> 00:06:27,200
You're right, it's once twice how many times unions and everything goes indented under the with line.

69
00:06:27,620 --> 00:06:34,250
So it's a bloc of codes, and the benefits here is that you don't need this close method because once

70
00:06:34,250 --> 00:06:39,380
the block is run, the file is closed automatically.

71
00:06:39,380 --> 00:06:42,800
So this is a recommended way to create and handle files.

72
00:06:43,160 --> 00:06:52,400
You don't need to use the closed methods, and normally you want to define the contents outside this

73
00:06:52,400 --> 00:06:52,820
block.

74
00:06:53,510 --> 00:06:55,730
So first, you have the contents variable.

75
00:06:55,850 --> 00:07:02,060
You know what you want to write, and then you apply the right methods and you say content there, and

76
00:07:02,060 --> 00:07:04,370
perhaps you need one more methods or not.

77
00:07:05,270 --> 00:07:06,260
Not in this case.

78
00:07:06,830 --> 00:07:08,480
So do you read that as well?

79
00:07:08,780 --> 00:07:12,650
That is the final codes for this video.

80
00:07:12,770 --> 00:07:13,640
So from that?

81
00:07:14,740 --> 00:07:18,100
And you should see that we've got this content.

82
00:07:19,210 --> 00:07:26,170
So that is the best way to write a new file in Python and add content to that file.

83
00:07:26,980 --> 00:07:31,330
In the next video, I'll show you how to read text files, which is very easy.

84
00:07:31,450 --> 00:07:31,970
See you there!

