Hey, welcome to the Cold Experiment video for today.
Today, we were able to read the contents of a file using the open function and the read lines method
as well.
And Python is able to find the file using this file name to do the trick.
So we simply give the name of the file here.
And that is the case when the file is located in the project root directory.
So the directory The root directory is app one.
And to do this under that directory.
But if to dos was in another folder, for example, if I drag to dos and drop it in another folder,
let's create a folder first.
Let's name it files.
So files now is inside app one and to dos the txt will be drag and dropped inside files.
Now.
When I press refactor here.
You'll see that the path here of this file.
Will be changed by power or automatically.
So take a look here, how it will change when I press refactor.
You see it changed from to dos that txt to files slash to dos the text.
So all the files the file paths changed to that.
So that was done automatically by part charm.
If part charm didn't do that, we would have to write this manually.
So files slash to do the text because otherwise if this was just to reduce the text, this as well.
And the other was also.
And we run the code and we try to add something.
So Kay's ad now doesn't have the file slash port.
So if I press add and I try to add it to do such as by fruits, we're going to get an error.
And the error says file not found error, no such file or directory to dos the text.
So because to dos is in another folder, you have to give the relative path to that folder.
And the relative path is file slash to dos.
Again, here for us, slash to dos.
So that is a relative path and it is relative to the project root directory, which in my case is app
one.
App.
One is located somewhere in my computer.
Maybe see slash users audits.
EP one, etc. It doesn't matter.
The absolute path here doesn't matter.
All these files can be referenced using relative paths from within Python.
So if we have, for example, another folder inside files, so let's create a new one.
Sup files and drag to the static C to sap SAP files.
Press refactor and par charm again will change the path to file SAP files to dos dot txt using slashes.
To separate directories from directories and directories from files.
It could also happen that you may need to point to a file which is outside of the project root directory.
In that case, you'd have to use an absolute path, and I'll show you how to do that.
Now.
The only thing is that the process is different between Mac Linux and Windows.
So for that, I'll show you now.
My windows.
Computer on top and my Mac computer at the bottom of the instructions for Mac will also be valid for
Linux users.
So the instructions for Windows that that's the Windows computer.
And let's suppose that you had a file in your downloads folder.
The file name is to DOS.
It also has an extension that text, but Windows doesn't show it.
However, the actual file name is to do the text.
Now you want to.
Click here and copy that path.
That is the absolute path you need to provide.
Now in Python, I don't want to pull you to my app here, so I'll just open a Python console.
So again, we would use open.
The function open and inside the codes you'd paste that directory.
And then you'd add.
Another backslash.
So not a slash on windows, but a back slash to dos does text.
And then you want to go to the beginning of the string outside the quotes and adds, Ah, I'm adding
our here because we have back slashes on Windows.
And that means some characters are special characters in Python, for example, Backslash T creates
a tab.
If you remember, Backslash GN was also a special character.
It created a brake line.
So when you when you say print backslash n, you get a break line.
If you say print backslash T, you get a tab for that reason.
We want to change this to a row string by adding our just before the quotes that will treat.
Every character as a normal character, it will ignore special characters and then add the ah argument
and press enter.
If you got an error, probably you may need to add another text here because perhaps when you created
the file you manually added a text, but then Windows adds another one on top of that.
So you end up with this file name actually.
So try to execute this and see if it works.
You know it works when this doesn't give you any output.
And you also know it works when you apply the read method and you get the contents of that file, which
is in my downloads folder.
So that's a file.
So that was how to read an absolute file on Windows.
On Mac and Linux.
Let's suppose we have this file to use the THC, which is in the downloads folder.
So you may see like that, but if you press this.
View option in here.
You're going to be able to trace where the directory comes from.
So you get users A s and downloads to do that.
So that means the path is.
File is open.
The path is slash.
We're using forward.
Slash is here on Windows.
You need to use back slashes here, you use forwards normal slashes users s and downloads slash to do
stuff.
Text press enter file the.
It gives me the contents of that file.
So you need to change this to your username.
So you need to actually be able to get the actual path on your own computer.
And that's how you read absolute paths on Windows and Mac or Linux.
So Mac and Linux use four slashes.
So those were some code experiments on file paths.
I hope you understand them better.
As I mentioned, most of the time you're going to have the files in your project directory, so you
you'll not have to bother with absolute paths, just relative paths.
So thank you.
See you later.