1
00:00:00,150 --> 00:00:09,930
Let's suppose a scenario where we have multiple Sears files and each of these files have an extra coma,

2
00:00:10,140 --> 00:00:16,920
which is unwanted, and we want to remove that coma of the ends of each file.

3
00:00:17,280 --> 00:00:19,750
And of course, we want to use Python to do that.

4
00:00:19,750 --> 00:00:25,980
So we want to automate the process instead of going through each file and deleting that it could be

5
00:00:25,980 --> 00:00:27,570
a thousand 2000 files.

6
00:00:27,900 --> 00:00:29,040
You don't want to do that.

7
00:00:29,370 --> 00:00:31,020
It's very easy to do with Python.

8
00:00:31,920 --> 00:00:39,330
So in this video, we're going to remove a comma, the lost character from a C as we file.

9
00:00:39,810 --> 00:00:44,070
But let's slow down a bit and let's work on one single file first.

10
00:00:44,340 --> 00:00:49,950
So I'm going to remove two of these files and just work on one of them in the next video.

11
00:00:49,950 --> 00:00:55,140
Then we will work on multiple files to remove the lost character of all the files.

12
00:00:55,740 --> 00:01:03,120
So see, as we files are just another type of text file, they happen to have a C USB extension, but

13
00:01:03,120 --> 00:01:05,190
you can open them in a text editor.

14
00:01:06,000 --> 00:01:14,010
So these are files that are separated by commas by separating these files by a comma.

15
00:01:14,040 --> 00:01:18,180
You have this well-defined structure to represent data.

16
00:01:18,420 --> 00:01:22,170
So, for example, we have this idea and so we have rows.

17
00:01:22,770 --> 00:01:26,400
34 is the idea for the first item?

18
00:01:26,490 --> 00:01:28,530
Fifty six is the idea for the next slide.

19
00:01:28,530 --> 00:01:30,930
Some five is the idea for the other item and so on.

20
00:01:31,290 --> 00:01:32,970
But then we want to remove this comma.

21
00:01:33,270 --> 00:01:36,820
Let's see how to do that in Python with open.

22
00:01:36,840 --> 00:01:40,770
Of course, we want to use the open methods file three that see us V..

23
00:01:40,800 --> 00:01:43,770
And what do we want to do?

24
00:01:44,400 --> 00:01:47,820
We want to get the contents of the file first.

25
00:01:48,300 --> 00:01:51,090
So we want to open the file in read mode.

26
00:01:52,360 --> 00:02:00,910
As foil and grab the contents with file that reads so we save the contents and the contents variable.

27
00:02:01,180 --> 00:02:03,100
Now see if we got that right.

28
00:02:03,280 --> 00:02:06,310
So on intent and print content.

29
00:02:08,150 --> 00:02:09,770
And yet, so that is the content.

30
00:02:10,009 --> 00:02:19,040
So this is a string, a plane, python string and as you know, with strings, we can do slice operations,

31
00:02:19,040 --> 00:02:27,260
for example, we can get all the characters, but the last one wrong, that's.

32
00:02:28,410 --> 00:02:36,600
And you see that the coma is not there anymore because a string has an indexing system embedded, so

33
00:02:36,600 --> 00:02:39,150
the less character has an index of minus one.

34
00:02:39,420 --> 00:02:42,570
That means the coma has an index of minus one.

35
00:02:42,930 --> 00:02:49,710
And when you do this, you're saying, give me all the items from the item with index zero, which is

36
00:02:49,920 --> 00:02:50,400
I.

37
00:02:51,570 --> 00:02:58,540
So that as an index of zero, this as an index of one to three, four, eight and so on.

38
00:02:58,890 --> 00:03:04,320
And you also have this a negative indexing so minus one minus two, minus three and so on.

39
00:03:04,560 --> 00:03:09,570
So we get a zero, we get one two three and minus one.

40
00:03:09,570 --> 00:03:11,360
So minus one is not included.

41
00:03:11,670 --> 00:03:12,860
You can run this again.

42
00:03:12,870 --> 00:03:13,950
You get the same thing.

43
00:03:14,220 --> 00:03:19,650
So that is the equivalent of that.

44
00:03:21,460 --> 00:03:26,620
So this is considered to be better because you avoid having to ride the zero there.

45
00:03:27,700 --> 00:03:30,160
Of course, if you right minus two there and run.

46
00:03:31,490 --> 00:03:34,150
The lost zero is going to go way.

47
00:03:34,190 --> 00:03:36,620
Also minus three.

48
00:03:38,970 --> 00:03:51,960
Like that and so on, so minus one is what we need, and we want to stall this results in a new variable.

49
00:03:52,440 --> 00:04:00,150
So let's say modified contents eagles to vet content with that slice.

50
00:04:01,380 --> 00:04:05,010
Some modified content now has no comma of the ends.

51
00:04:06,300 --> 00:04:08,880
What do we want to do with the modified content?

52
00:04:08,890 --> 00:04:13,320
Stream now is open a new file.

53
00:04:13,740 --> 00:04:15,810
Now here you have to take a decision.

54
00:04:16,320 --> 00:04:23,130
You either ride the same file name or file that see as V as here.

55
00:04:23,430 --> 00:04:32,370
And that means you will override this file with the new content, which does not have a comma, or you

56
00:04:32,370 --> 00:04:35,320
can create a new file to create a new file.

57
00:04:35,340 --> 00:04:41,640
All you have to do is give a new name to the new file, and that means you will not override the existing

58
00:04:41,640 --> 00:04:41,940
file.

59
00:04:42,510 --> 00:04:44,610
So like, that's in right mode.

60
00:04:45,820 --> 00:04:46,570
S file.

61
00:04:48,710 --> 00:04:53,810
Five, that's right, modify its contents, execute.

62
00:04:55,660 --> 00:04:58,030
And this is a new file without a comma.

63
00:04:59,460 --> 00:05:00,640
And that is the coat.

64
00:05:01,710 --> 00:05:03,900
Let's try by overwriting the file.

65
00:05:04,200 --> 00:05:06,450
File three The case we run.

66
00:05:07,990 --> 00:05:12,130
Now you go to five three doses and you don't see the coma anymore.

67
00:05:14,120 --> 00:05:20,330
Whether you overrides or creates a new file that that is up to you, I would suggest that if you want

68
00:05:20,330 --> 00:05:26,510
to override the existing file, perhaps you want to make a copy first, just copy the folder where your

69
00:05:26,510 --> 00:05:32,390
files are and put it somewhere else just to back it up in case you screw things with Python.

70
00:05:33,020 --> 00:05:38,210
So that was an example on using file reading and writing.

71
00:05:38,510 --> 00:05:46,580
In practical example, in the next video, we will remove the comma from multiple files at once.

72
00:05:46,730 --> 00:05:47,450
So sue there.

