1
00:00:00,840 --> 00:00:06,180
So far in this revision, we have worked with code which processes internal information.

2
00:00:06,180 --> 00:00:08,910
So information inside the script itself.

3
00:00:08,910 --> 00:00:11,610
So getting a string from a variable, capitalizing it.

4
00:00:13,050 --> 00:00:16,410
Maybe getting some input from a function processing it.

5
00:00:16,740 --> 00:00:24,690
So the python file was not communicating with any external files, but that is not always the case in

6
00:00:24,690 --> 00:00:25,500
real life.

7
00:00:28,030 --> 00:00:32,200
So in real applications, you have to process external files.

8
00:00:32,259 --> 00:00:34,480
That's why now we need to review.

9
00:00:36,470 --> 00:00:41,690
External file processing, and we're working with text files which are the most common.

10
00:00:43,160 --> 00:00:44,240
Type of files.

11
00:00:46,700 --> 00:00:48,170
So creating files.

12
00:00:48,170 --> 00:00:51,000
How can you create a file from Python?

13
00:00:51,020 --> 00:00:52,700
Well, using that code.

14
00:00:55,660 --> 00:01:04,360
So the open function will create a file object and that file object will be stored in this variable.

15
00:01:04,360 --> 00:01:04,959
Right?

16
00:01:05,770 --> 00:01:09,440
So file will be equal to this file object.

17
00:01:09,460 --> 00:01:13,720
Then in this file object, you have to write contents.

18
00:01:13,720 --> 00:01:15,250
So the right method.

19
00:01:17,380 --> 00:01:20,380
Belongs to this type of object.

20
00:01:20,380 --> 00:01:27,580
So it's part of the file object type and X, and it expects a string.

21
00:01:27,760 --> 00:01:33,100
So if I run that, that will create a bulk text file.

22
00:01:35,030 --> 00:01:37,220
So this is a case of writing files.

23
00:01:37,220 --> 00:01:44,690
And the key here is that you need to provide this y argument for writing.

24
00:01:45,260 --> 00:01:51,080
Now, another version of writing files, writing content to a file is this year.

25
00:01:51,140 --> 00:01:55,580
So this was just a one line string, a simple string.

26
00:01:56,120 --> 00:01:58,490
But sometimes you may want to add.

27
00:01:59,340 --> 00:02:02,370
Strings which span across multiple lines.

28
00:02:02,460 --> 00:02:09,750
In that case, you want to use a multi-line string with triple quotes here and triple quotes there.

29
00:02:09,780 --> 00:02:13,710
You store that in variable and then you apply that code again.

30
00:02:15,440 --> 00:02:21,290
And the only difference here is that you are providing the valuable content here instead of writing

31
00:02:21,290 --> 00:02:23,300
the string directly.

32
00:02:23,870 --> 00:02:26,270
You can also write the string directly.

33
00:02:26,270 --> 00:02:32,060
You can cut that and paste it here, but that is not very readable.

34
00:02:33,290 --> 00:02:35,060
It would mess up the culture.

35
00:02:36,730 --> 00:02:43,150
So whenever you have multi-line strings, please store them in a variable first and then use that variable

36
00:02:44,500 --> 00:02:45,460
in your code.

37
00:02:47,230 --> 00:02:53,900
Something to note is that if I was to run this script, it would create this book, that text file.

38
00:02:53,950 --> 00:02:54,520
Right.

39
00:02:55,090 --> 00:03:02,500
And then if I run this other script, this would override that book, the text file.

40
00:03:04,020 --> 00:03:08,060
That is because the name here and the name in here are the same.

41
00:03:08,070 --> 00:03:11,190
So this would overwrite that previous file.

42
00:03:11,310 --> 00:03:19,110
And another thing to note is that the files will be created in the project directory.

43
00:03:20,280 --> 00:03:28,650
So if you are working on Parcham, you have a dot p file where you have the code and this file will

44
00:03:28,650 --> 00:03:32,880
be created in the same folder with your dot p file.

45
00:03:33,210 --> 00:03:39,870
But if you're using a python console, then it's important to know where the python console is pointing

46
00:03:39,870 --> 00:03:43,720
to what directory it is related to.

47
00:03:43,740 --> 00:03:45,540
What did you open it from?

48
00:03:46,380 --> 00:03:51,330
So the current directory of the Python console is important here because that is where the file will

49
00:03:51,330 --> 00:03:54,240
be created in that directory.

50
00:03:55,730 --> 00:03:57,620
Then we jumped to reading files.

51
00:03:58,520 --> 00:04:02,720
The difference here is the arguments and the right methods.

52
00:04:03,680 --> 00:04:10,100
And also, of course, here we store the output of this in this variable.

53
00:04:11,820 --> 00:04:18,779
So this returns a string and the string represents the text of that file.

54
00:04:18,779 --> 00:04:26,160
So this file has to exist in the current directory, in the project directory for you to be able to

55
00:04:26,160 --> 00:04:26,790
read it.

56
00:04:27,300 --> 00:04:32,340
So for example, here, if I call this variable, I'll get this.

57
00:04:35,290 --> 00:04:35,800
Right.

58
00:04:38,170 --> 00:04:41,110
That is the text that I wrote in here.

59
00:04:41,110 --> 00:04:41,820
Up here?

60
00:04:41,890 --> 00:04:42,640
No, no.

61
00:04:42,640 --> 00:04:47,260
That we have these backslash and they symbolize break lines.

62
00:04:47,530 --> 00:04:52,000
So when you just call the variable like that in a Python console.

63
00:04:53,640 --> 00:05:00,390
You may see these as backslash ends, but if you use a print function to print that variable, then

64
00:05:00,390 --> 00:05:04,880
the backslash ends will be rendered as actual break lines.

65
00:05:04,890 --> 00:05:09,420
So we have a break line in here and we have a break line in here as well.

66
00:05:12,860 --> 00:05:16,040
Lastly, the red Lines method.

67
00:05:16,850 --> 00:05:21,380
So you see that the difference of this and that here.

68
00:05:23,190 --> 00:05:24,690
Is the methods.

69
00:05:24,720 --> 00:05:25,900
This is red lines.

70
00:05:25,920 --> 00:05:26,340
This is.

71
00:05:26,940 --> 00:05:30,870
And of course, this time I'm reading another text file.

72
00:05:34,110 --> 00:05:37,440
I did create this text file up here.

73
00:05:40,030 --> 00:05:42,010
Using the right lines method.

74
00:05:43,570 --> 00:05:47,260
So right lines is the homologue of red lines.

75
00:05:47,260 --> 00:05:52,420
Basically, with rights lines, you provides a list of strings.

76
00:05:54,480 --> 00:05:57,480
But with rights, you provide a string.

77
00:05:58,860 --> 00:06:05,670
So write lines will write each of those things in a separate line in the text file.

78
00:06:05,850 --> 00:06:14,520
And red lines will read every line as a separate string and produce a list of those lines.

79
00:06:16,200 --> 00:06:24,180
It's important to use a brake line after these lines in here if you want them to be separated in separate

80
00:06:24,180 --> 00:06:24,870
lines.

81
00:06:25,180 --> 00:06:25,700
Right.

82
00:06:25,830 --> 00:06:30,550
Then when you read this, of course you'll read it together with those backslash ends.

83
00:06:30,570 --> 00:06:32,640
As you see here, down here.

84
00:06:33,600 --> 00:06:38,370
So if you want to get rid of those ends, you can use list comprehension.

85
00:06:41,720 --> 00:06:50,210
So what I'm doing here is I am iterating through the items of the content variable of this list through

86
00:06:50,210 --> 00:06:51,650
each of these strings.

87
00:06:52,580 --> 00:07:02,620
So for each string in the content list, I am stripping out the string from those backslash ends.

88
00:07:02,630 --> 00:07:05,600
So strip will remove those characters.

89
00:07:07,500 --> 00:07:09,420
And so clean content.

90
00:07:10,180 --> 00:07:12,010
Will contain this list now.

91
00:07:12,010 --> 00:07:16,060
So clean content is the variable that we created in here.

92
00:07:18,980 --> 00:07:20,750
And that's about this video.

93
00:07:22,270 --> 00:07:24,110
We have one more and we are done.

94
00:07:24,130 --> 00:07:25,180
So assuming the next one.

