1
00:00:00,300 --> 00:00:03,300
Hey, welcome to the Cold Experiment video for today.

2
00:00:06,350 --> 00:00:16,100
Today, we were able to read the contents of a file using the open function and the read lines method

3
00:00:16,100 --> 00:00:16,820
as well.

4
00:00:17,300 --> 00:00:23,560
And Python is able to find the file using this file name to do the trick.

5
00:00:24,500 --> 00:00:27,260
So we simply give the name of the file here.

6
00:00:29,030 --> 00:00:35,120
And that is the case when the file is located in the project root directory.

7
00:00:35,240 --> 00:00:38,300
So the directory The root directory is app one.

8
00:00:39,130 --> 00:00:41,950
And to do this under that directory.

9
00:00:42,220 --> 00:00:50,740
But if to dos was in another folder, for example, if I drag to dos and drop it in another folder,

10
00:00:50,770 --> 00:00:52,480
let's create a folder first.

11
00:00:54,840 --> 00:00:56,160
Let's name it files.

12
00:00:56,160 --> 00:01:04,440
So files now is inside app one and to dos the txt will be drag and dropped inside files.

13
00:01:05,920 --> 00:01:06,490
Now.

14
00:01:07,880 --> 00:01:10,460
When I press refactor here.

15
00:01:11,490 --> 00:01:14,460
You'll see that the path here of this file.

16
00:01:15,610 --> 00:01:18,730
Will be changed by power or automatically.

17
00:01:19,240 --> 00:01:22,510
So take a look here, how it will change when I press refactor.

18
00:01:22,870 --> 00:01:29,560
You see it changed from to dos that txt to files slash to dos the text.

19
00:01:29,770 --> 00:01:33,880
So all the files the file paths changed to that.

20
00:01:35,350 --> 00:01:37,900
So that was done automatically by part charm.

21
00:01:37,900 --> 00:01:42,280
If part charm didn't do that, we would have to write this manually.

22
00:01:42,280 --> 00:01:52,810
So files slash to do the text because otherwise if this was just to reduce the text, this as well.

23
00:01:52,810 --> 00:01:55,450
And the other was also.

24
00:01:57,880 --> 00:02:02,320
And we run the code and we try to add something.

25
00:02:02,680 --> 00:02:09,449
So Kay's ad now doesn't have the file slash port.

26
00:02:09,460 --> 00:02:18,250
So if I press add and I try to add it to do such as by fruits, we're going to get an error.

27
00:02:18,250 --> 00:02:24,640
And the error says file not found error, no such file or directory to dos the text.

28
00:02:27,080 --> 00:02:35,120
So because to dos is in another folder, you have to give the relative path to that folder.

29
00:02:35,120 --> 00:02:38,570
And the relative path is file slash to dos.

30
00:02:38,750 --> 00:02:41,840
Again, here for us, slash to dos.

31
00:02:43,130 --> 00:02:50,930
So that is a relative path and it is relative to the project root directory, which in my case is app

32
00:02:50,930 --> 00:02:51,650
one.

33
00:02:53,090 --> 00:02:53,430
App.

34
00:02:53,450 --> 00:02:57,140
One is located somewhere in my computer.

35
00:02:58,330 --> 00:03:02,170
Maybe see slash users audits.

36
00:03:03,330 --> 00:03:05,580
EP one, etc. It doesn't matter.

37
00:03:05,580 --> 00:03:07,570
The absolute path here doesn't matter.

38
00:03:07,590 --> 00:03:16,590
All these files can be referenced using relative paths from within Python.

39
00:03:17,850 --> 00:03:23,610
So if we have, for example, another folder inside files, so let's create a new one.

40
00:03:25,620 --> 00:03:31,350
Sup files and drag to the static C to sap SAP files.

41
00:03:31,770 --> 00:03:42,450
Press refactor and par charm again will change the path to file SAP files to dos dot txt using slashes.

42
00:03:45,000 --> 00:03:51,840
To separate directories from directories and directories from files.

43
00:03:53,490 --> 00:04:00,450
It could also happen that you may need to point to a file which is outside of the project root directory.

44
00:04:02,200 --> 00:04:07,220
In that case, you'd have to use an absolute path, and I'll show you how to do that.

45
00:04:07,240 --> 00:04:07,780
Now.

46
00:04:09,780 --> 00:04:17,820
The only thing is that the process is different between Mac Linux and Windows.

47
00:04:17,970 --> 00:04:19,680
So for that, I'll show you now.

48
00:04:20,740 --> 00:04:22,000
My windows.

49
00:04:22,720 --> 00:04:31,450
Computer on top and my Mac computer at the bottom of the instructions for Mac will also be valid for

50
00:04:31,450 --> 00:04:32,410
Linux users.

51
00:04:34,820 --> 00:04:40,160
So the instructions for Windows that that's the Windows computer.

52
00:04:41,120 --> 00:04:46,250
And let's suppose that you had a file in your downloads folder.

53
00:04:48,190 --> 00:04:49,990
The file name is to DOS.

54
00:04:51,290 --> 00:04:55,010
It also has an extension that text, but Windows doesn't show it.

55
00:04:55,030 --> 00:04:58,480
However, the actual file name is to do the text.

56
00:04:58,840 --> 00:05:00,010
Now you want to.

57
00:05:01,150 --> 00:05:03,640
Click here and copy that path.

58
00:05:05,200 --> 00:05:07,650
That is the absolute path you need to provide.

59
00:05:07,660 --> 00:05:14,950
Now in Python, I don't want to pull you to my app here, so I'll just open a Python console.

60
00:05:21,490 --> 00:05:24,460
So again, we would use open.

61
00:05:25,650 --> 00:05:31,260
The function open and inside the codes you'd paste that directory.

62
00:05:32,660 --> 00:05:33,980
And then you'd add.

63
00:05:35,320 --> 00:05:36,730
Another backslash.

64
00:05:36,730 --> 00:05:43,840
So not a slash on windows, but a back slash to dos does text.

65
00:05:45,910 --> 00:05:52,930
And then you want to go to the beginning of the string outside the quotes and adds, Ah, I'm adding

66
00:05:52,930 --> 00:05:57,190
our here because we have back slashes on Windows.

67
00:05:57,250 --> 00:06:03,490
And that means some characters are special characters in Python, for example, Backslash T creates

68
00:06:03,490 --> 00:06:04,390
a tab.

69
00:06:05,890 --> 00:06:09,130
If you remember, Backslash GN was also a special character.

70
00:06:09,130 --> 00:06:10,860
It created a brake line.

71
00:06:10,870 --> 00:06:14,760
So when you when you say print backslash n, you get a break line.

72
00:06:14,770 --> 00:06:21,340
If you say print backslash T, you get a tab for that reason.

73
00:06:21,550 --> 00:06:28,720
We want to change this to a row string by adding our just before the quotes that will treat.

74
00:06:30,140 --> 00:06:39,560
Every character as a normal character, it will ignore special characters and then add the ah argument

75
00:06:40,070 --> 00:06:42,290
and press enter.

76
00:06:43,010 --> 00:06:51,110
If you got an error, probably you may need to add another text here because perhaps when you created

77
00:06:51,110 --> 00:06:56,120
the file you manually added a text, but then Windows adds another one on top of that.

78
00:06:56,120 --> 00:06:58,580
So you end up with this file name actually.

79
00:06:58,790 --> 00:07:03,260
So try to execute this and see if it works.

80
00:07:03,560 --> 00:07:07,340
You know it works when this doesn't give you any output.

81
00:07:07,340 --> 00:07:15,470
And you also know it works when you apply the read method and you get the contents of that file, which

82
00:07:15,470 --> 00:07:17,270
is in my downloads folder.

83
00:07:18,580 --> 00:07:19,810
So that's a file.

84
00:07:25,780 --> 00:07:30,730
So that was how to read an absolute file on Windows.

85
00:07:31,970 --> 00:07:33,320
On Mac and Linux.

86
00:07:33,320 --> 00:07:39,590
Let's suppose we have this file to use the THC, which is in the downloads folder.

87
00:07:41,060 --> 00:07:44,120
So you may see like that, but if you press this.

88
00:07:45,190 --> 00:07:47,050
View option in here.

89
00:07:47,080 --> 00:07:51,850
You're going to be able to trace where the directory comes from.

90
00:07:51,850 --> 00:07:57,970
So you get users A s and downloads to do that.

91
00:07:58,480 --> 00:08:00,910
So that means the path is.

92
00:08:04,780 --> 00:08:06,760
File is open.

93
00:08:07,000 --> 00:08:10,660
The path is slash.

94
00:08:10,660 --> 00:08:11,890
We're using forward.

95
00:08:11,890 --> 00:08:13,180
Slash is here on Windows.

96
00:08:13,180 --> 00:08:25,990
You need to use back slashes here, you use forwards normal slashes users s and downloads slash to do

97
00:08:25,990 --> 00:08:26,290
stuff.

98
00:08:26,290 --> 00:08:30,640
Text press enter file the.

99
00:08:30,940 --> 00:08:33,940
It gives me the contents of that file.

100
00:08:34,659 --> 00:08:37,510
So you need to change this to your username.

101
00:08:37,510 --> 00:08:42,580
So you need to actually be able to get the actual path on your own computer.

102
00:08:43,840 --> 00:08:49,900
And that's how you read absolute paths on Windows and Mac or Linux.

103
00:08:50,920 --> 00:08:55,000
So Mac and Linux use four slashes.

104
00:08:56,080 --> 00:08:59,920
So those were some code experiments on file paths.

105
00:08:59,950 --> 00:09:01,900
I hope you understand them better.

106
00:09:03,290 --> 00:09:08,590
As I mentioned, most of the time you're going to have the files in your project directory, so you

107
00:09:08,820 --> 00:09:15,770
you'll not have to bother with absolute paths, just relative paths.

108
00:09:16,820 --> 00:09:17,510
So thank you.

109
00:09:17,540 --> 00:09:18,110
See you later.

