#65 Python Tutorial for Beginners | File handling

00:12:13
https://www.youtube.com/watch?v=aequTxAvQq4

Ringkasan

TLDRVideoen gir en grundig innføring i filhåndtering i Python, inkludert hvordan man åpner, leser, skriver og kopierer filer. Den forklarer viktigheten av å lagre data permanent og demonstrerer bruken av tekst- og binære filer. Seeren lærer om ulike moduser for filhåndtering, som 'r', 'w', og 'a', samt metoder som 'read', 'readline', og 'write'. Eksempler på praktisk bruk av disse metodene vises, inkludert hvordan man håndterer feil og spesifikasjoner for binære filer.

Takeaways

  • 📁 Filer er viktige for permanent datalagring.
  • 📝 Bruk 'open' for å åpne filer med spesifikke moduser.
  • 📖 'read' leser hele filen, mens 'readline' leser én linje.
  • ✍️ 'write' overskriver eksisterende data, 'append' legger til.
  • 🔄 Bruk 'for loop' for å iterere gjennom filinnhold.
  • 🔍 'rb' brukes for å lese binære filer som bilder.
  • 📂 Du kan kopiere innhold fra en fil til en annen.
  • 🚫 Vær oppmerksom på at 'w' vil slette eksisterende data.
  • 🔧 Bruk 'end' i print for å unngå ekstra linjeskift.
  • 📊 Filer kan inneholde både tekst og binære data.

Garis waktu

  • 00:00:00 - 00:05:00

    I denne videoen fortsetter vi med Python og fokuserer på filer. Vi diskuterer hvorfor filer er nødvendige for å lagre data på en vedvarende måte, i motsetning til midlertidige variabler. Vi lærer hvordan man åpner filer i Python ved hjelp av den innebygde 'open'-funksjonen, og hvordan man spesifiserer modusen for å lese eller skrive til filer. Vi ser på hvordan man kan lese innholdet i en fil, inkludert å hente spesifikke linjer, og hvordan man kan håndtere linjeskift ved utskrift.

  • 00:05:00 - 00:12:13

    Vi går videre til å skrive data til filer, inkludert hvordan man oppretter en ny fil og legger til innhold. Vi lærer om forskjellen mellom 'write' og 'append' moduser, og hvordan man kan kopiere innhold fra en fil til en annen ved hjelp av løkker. Til slutt ser vi på hvordan man håndterer binære filer, som bilder, og hvordan man kan lese og skrive disse filene i binærmodus. Videoen avsluttes med en oppfordring til seerne om å stille spørsmål og abonnere.

Peta Pikiran

Video Tanya Jawab

  • Hvorfor trenger vi filer i Python?

    Filer brukes til å lagre data permanent, slik at det ikke går tapt når programmet lukkes.

  • Hvordan åpner jeg en fil i Python?

    Bruk 'open' funksjonen med filnavn og modus som parametere.

  • Hva er forskjellen mellom 'r', 'w' og 'a' moduser?

    'r' er for å lese, 'w' er for å skrive (overskriver eksisterende data), og 'a' er for å legge til data.

  • Hvordan leser jeg en linje fra en fil?

    Bruk 'readline' metoden for å lese en enkelt linje.

  • Hvordan skriver jeg data til en fil?

    Bruk 'write' metoden etter å ha åpnet filen i skrive- eller append-modus.

  • Kan jeg lese binære filer i Python?

    Ja, bruk 'rb' for å åpne en fil i binær modus.

  • Hvordan kopierer jeg innhold fra en fil til en annen?

    Les innholdet fra den første filen og skriv det til den andre filen ved hjelp av en løkke.

  • Hva skjer hvis jeg åpner en fil med 'w' modus når filen ikke eksisterer?

    Python vil opprette en ny fil.

  • Hvordan unngår jeg ekstra linjeskift når jeg skriver ut data?

    Bruk 'end' parameteren i print funksjonen.

  • Hva er 'for loop' i filbehandling?

    En 'for loop' kan brukes til å iterere gjennom hver linje i en fil.

Lihat lebih banyak ringkasan video

Dapatkan akses instan ke ringkasan video YouTube gratis yang didukung oleh AI!
Teks
en
Gulir Otomatis:
  • 00:00:03
    welcome back aliens my name is 720 and
  • 00:00:06
    let's continue with the series on Python
  • 00:00:08
    in this video we will try to focus on
  • 00:00:12
    files but why do we need files in the
  • 00:00:14
    first place
  • 00:00:15
    think about this let's say if you are
  • 00:00:17
    working with a code and of course when
  • 00:00:19
    you write a code you will use certain
  • 00:00:20
    variables and in those variables you
  • 00:00:23
    will be storing some data now most of
  • 00:00:25
    the data which we use in variables are
  • 00:00:27
    temporary data right if you want to
  • 00:00:29
    count a variable if you want to store
  • 00:00:31
    some data like temporary data you will
  • 00:00:33
    store that in a variable but what if you
  • 00:00:36
    want to save a data in a persistent way
  • 00:00:38
    no I mean simply for a longer period now
  • 00:00:42
    of course the moment you close the
  • 00:00:43
    application you will lose all the data
  • 00:00:45
    what I want is even if I close the
  • 00:00:47
    application I want my data to be stored
  • 00:00:50
    somewhere and that's where you need to
  • 00:00:53
    find a permanent storage one of the way
  • 00:00:55
    you can do that is by using a relational
  • 00:00:58
    database example you can use MySQL you
  • 00:01:00
    can use Oracle but then it also provides
  • 00:01:03
    you a table structure right and those
  • 00:01:05
    are complex stuff what if you want to
  • 00:01:08
    store something for a longer period but
  • 00:01:09
    then in a simple format and the best way
  • 00:01:12
    to go for is text so let's imagine if we
  • 00:01:14
    have a text file here which is my data
  • 00:01:16
    and inside this my data I have some data
  • 00:01:18
    you can see it's my name there there is
  • 00:01:20
    code channel name and it's a programming
  • 00:01:23
    channel and all those stuffs right so we
  • 00:01:24
    have this data random data and I want to
  • 00:01:27
    use this data in a code so how do I open
  • 00:01:30
    this file in our code
  • 00:01:32
    I know it seems difficult by the moment
  • 00:01:34
    you say hey how can you open external
  • 00:01:36
    file in our code back of your mind you
  • 00:01:38
    might be thinking oh it will be very
  • 00:01:39
    complex but don't worry as we mentioned
  • 00:01:41
    before python is the easiest language
  • 00:01:43
    and to do all this stuff we already have
  • 00:01:45
    some inbuilt functions so let's do that
  • 00:01:48
    so one of the function or method you can
  • 00:01:51
    use here is known as open so you simply
  • 00:01:54
    say open you have to pass two basic
  • 00:01:56
    parameters here the first one is the
  • 00:01:58
    file name itself and the file name here
  • 00:02:00
    is my data okay so you have to make sure
  • 00:02:03
    the file name do we have the extension
  • 00:02:05
    for this I don't think so
  • 00:02:07
    so have to mention the file name and you
  • 00:02:09
    need to also mention the mode now it is
  • 00:02:13
    mode here see when I work with a file of
  • 00:02:15
    course there are different power
  • 00:02:17
    you can use files one of these will be
  • 00:02:18
    used file is to rate the file one reason
  • 00:02:21
    to open a file is to write something in
  • 00:02:24
    it or maybe to overwrite something in it
  • 00:02:26
    so there are different purpose right to
  • 00:02:27
    open a file in the same way here when
  • 00:02:29
    you open a file you have to show your
  • 00:02:31
    purpose do you want to read a file do
  • 00:02:33
    you want to write a file now you want to
  • 00:02:35
    thinking why does it even matter if I
  • 00:02:37
    open a file I want to do whatever I want
  • 00:02:39
    see the thing is when you work on a
  • 00:02:41
    complex system you might be working with
  • 00:02:43
    multiple threads you want to give
  • 00:02:45
    certain permissions and to read a file
  • 00:02:47
    we may achieve multi-threading but to
  • 00:02:50
    write a file you have to make sure we
  • 00:02:51
    are using only one thread or something
  • 00:02:53
    like that
  • 00:02:54
    again that's a complex stuff but time
  • 00:02:56
    being imagine whenever you open a file
  • 00:02:58
    it's very important for a system to know
  • 00:02:59
    what your intentions are now how do you
  • 00:03:02
    mention your intention it's very simple
  • 00:03:04
    you just need to use odd here the moment
  • 00:03:06
    you say our it means you are opening
  • 00:03:09
    this file for greeting okay so we have
  • 00:03:11
    done with the opening your file but then
  • 00:03:13
    if you want to handle that file you will
  • 00:03:15
    save that file somewhere so you will say
  • 00:03:16
    F is equal to open so you can imagine
  • 00:03:19
    this F is a file now it's not actually
  • 00:03:21
    it's a simply object or Die reference
  • 00:03:23
    but you can imagine okay so time when
  • 00:03:25
    you can say this is a file now with this
  • 00:03:27
    file you can do certain operations
  • 00:03:28
    example I want to print file let's see
  • 00:03:31
    what happens when you print F as it is
  • 00:03:33
    so I'm printing F here let's see what
  • 00:03:35
    what it says let's run this demo and you
  • 00:03:37
    can see F is of type text I of the
  • 00:03:40
    wrapper with name ma Gator with a mole
  • 00:03:43
    are so it is giving you all different
  • 00:03:45
    features so it is giving you everything
  • 00:03:47
    except one thing which is the data of
  • 00:03:49
    course how do you fetch data it's very
  • 00:03:51
    simple actually so this open in fact
  • 00:03:53
    when you talk about files it provides
  • 00:03:54
    you certain methods to use what are the
  • 00:03:57
    methods which we can use here is reading
  • 00:03:59
    so we can use read to fetch the data
  • 00:04:02
    let's see what happens now I'm using
  • 00:04:04
    read and let's run this code oh we got
  • 00:04:07
    everything so it's so simple to print a
  • 00:04:09
    data from a file in Python now what if
  • 00:04:11
    you don't want to print the entire file
  • 00:04:14
    you want to print only one line the
  • 00:04:15
    first line do we have an option so the
  • 00:04:18
    moment you say control space you can see
  • 00:04:20
    so it has certain names we have read we
  • 00:04:23
    have read line and then we have read
  • 00:04:25
    lines
  • 00:04:25
    okay so let's use read line here now
  • 00:04:28
    when they said read line and let's see
  • 00:04:30
    what happens
  • 00:04:31
    read line okay just next to mine so you
  • 00:04:32
    can see when you say read it will print
  • 00:04:34
    everything when you said we'd line and
  • 00:04:36
    that's one this code you can see it only
  • 00:04:38
    prints the first line
  • 00:04:39
    oh okay so that means when you use red
  • 00:04:41
    line it will print on the first line
  • 00:04:44
    what if I want to print the second line
  • 00:04:46
    so it's very simple just copy this code
  • 00:04:48
    so every time you said read it has a
  • 00:04:51
    inbuilt pointer inside it so when you
  • 00:04:53
    say read line the pointer will move to
  • 00:04:55
    the second line now and then when you
  • 00:04:56
    say to read right now it will fetch the
  • 00:04:58
    second line it will the pointer will
  • 00:04:59
    move to the third line let's verify so
  • 00:05:01
    let's run this code you can see we are
  • 00:05:03
    getting my name is know already and
  • 00:05:05
    second line is the disco on YouTube now
  • 00:05:07
    you are you thinking why we got this
  • 00:05:09
    space here it's because print itself
  • 00:05:11
    will give you a new line and in this
  • 00:05:13
    data as well you have new line right so
  • 00:05:15
    after every line you have a new line
  • 00:05:16
    plus print will also give you a new line
  • 00:05:18
    we don't want a new line after print so
  • 00:05:21
    we'll say and so let's put a symbol by
  • 00:05:23
    mentioning hash let's say when you
  • 00:05:25
    search it means we have done with the
  • 00:05:27
    line 1 and line 2 okay so you can see
  • 00:05:29
    this is what it does it says hash okay
  • 00:05:32
    so we should be doing that before as
  • 00:05:33
    well by its time let's move that so yes
  • 00:05:36
    so the idea is when you say when he does
  • 00:05:37
    put and it will by default just newline
  • 00:05:39
    so now we know how to work with red line
  • 00:05:42
    and read can we mention the line number
  • 00:05:44
    okay that's a question let's see if I
  • 00:05:46
    mentioned line number four and if I
  • 00:05:48
    don't use second one let's see what
  • 00:05:49
    happens we accept it
  • 00:05:51
    oh it is only printing for characters
  • 00:05:53
    okay so when you say four it will print
  • 00:05:55
    only four characters that's great so you
  • 00:05:57
    can use this function and is how it
  • 00:05:59
    works but how do we write data so we
  • 00:06:01
    know how to read now I want to know how
  • 00:06:03
    to write the data so in order to write
  • 00:06:05
    the data first of all let me remove that
  • 00:06:06
    C we cannot use our to write something
  • 00:06:09
    we have to use W to write some things
  • 00:06:11
    what I do is let me use another file
  • 00:06:12
    here we'll say f1 and let me open that
  • 00:06:15
    file and you have to mention the file
  • 00:06:17
    name here as well so I will say the file
  • 00:06:19
    name here is ABC and we have to mention
  • 00:06:22
    the right commands the moment you say w
  • 00:06:25
    it means you are trying to write
  • 00:06:27
    something in a file but hold on if you
  • 00:06:29
    see on the left hand side we don't have
  • 00:06:31
    any file as ABC now what will happen so
  • 00:06:34
    now it is smart here so when you say
  • 00:06:35
    open a file with a write more it will
  • 00:06:38
    first check do we have a file there so
  • 00:06:40
    we don't have a file right so it will
  • 00:06:41
    create a file for you let me show you so
  • 00:06:43
    if I run this code
  • 00:06:44
    you can see we are getting a new file
  • 00:06:46
    which is ABC and if I open this file yes
  • 00:06:49
    it's a text format you can see we don't
  • 00:06:50
    have any data of course we don't have a
  • 00:06:52
    data there right so let's ring something
  • 00:06:54
    and the way you can print data is by
  • 00:06:57
    saying f1 dot and you can use a write
  • 00:07:00
    command so you can say it right and here
  • 00:07:03
    you can write something I will say
  • 00:07:04
    something ok let's see what happens now
  • 00:07:07
    so let's run this code and you can see
  • 00:07:09
    we are not printing anything in the
  • 00:07:10
    console that's why we're not getting any
  • 00:07:11
    stuff here let's go back to ABC and you
  • 00:07:13
    can see a DC we got something so you can
  • 00:07:15
    write multiple data here you can also
  • 00:07:17
    write something let's say f1 dot and you
  • 00:07:20
    will say people let's run this code and
  • 00:07:22
    you can see in ABC we to get something
  • 00:07:24
    and people of course you can give a
  • 00:07:26
    space in between while writing this code
  • 00:07:28
    ok so this makes sense right
  • 00:07:31
    ok so what if if I say f1 dot right so I
  • 00:07:35
    know that in my file we already have
  • 00:07:37
    something and people I want to say
  • 00:07:40
    something people laptop so in my file I
  • 00:07:42
    want to also add a laptop how do I do
  • 00:07:44
    that so here you will simply say laptop
  • 00:07:47
    right that's what we are expecting here
  • 00:07:49
    but the twist is if I have this code
  • 00:07:50
    look at the output look at the file can
  • 00:07:53
    you see that we lost all the previous
  • 00:07:55
    data right so we lost something we lost
  • 00:07:57
    people we only have laptop now it's
  • 00:08:00
    because when you say W it simply means
  • 00:08:02
    write define what if we don't want to
  • 00:08:05
    write a file you want to append
  • 00:08:06
    something if I said let's say here if I
  • 00:08:08
    say mobile I want to obtain the moment
  • 00:08:10
    after laptop in that case you will open
  • 00:08:12
    a file with a so a means a painter so W
  • 00:08:15
    is right artists raid a is a painter and
  • 00:08:18
    now let's run this code and if I go back
  • 00:08:20
    to ABC you can see we have laptop and
  • 00:08:22
    mobile so remember this thing we have
  • 00:08:24
    our we have W and we have a ok now what
  • 00:08:27
    I wanna do is we have two files right we
  • 00:08:29
    have ABC and we have my data what our do
  • 00:08:32
    is I want to copy everything from my
  • 00:08:34
    data I want to save that in ABC that's a
  • 00:08:36
    difficult step right so first read all
  • 00:08:38
    the data from my data and then write
  • 00:08:41
    everything in ABC two steps first of all
  • 00:08:43
    let's understand how do you fetch
  • 00:08:45
    everything from my data we have seen one
  • 00:08:47
    way right we can use red line but then
  • 00:08:49
    how many times you will say we'd line
  • 00:08:51
    how do we know when your file will get
  • 00:08:53
    over that will be impossible to track so
  • 00:08:56
    the best way is you can actually use
  • 00:08:58
    Ford lobe and you say for data in F
  • 00:09:01
    right so what it will do is it will run
  • 00:09:03
    a loop and on a file F of course it will
  • 00:09:05
    run a loop and will try to fetch
  • 00:09:07
    everything one by one so if you can see
  • 00:09:09
    if I print if I print data one by one
  • 00:09:12
    and look at the output it will print
  • 00:09:13
    everything we have extra line then right
  • 00:09:15
    so but timed ignore that it the
  • 00:09:16
    important thing is it is fetching
  • 00:09:17
    everything one by one so if you have to
  • 00:09:19
    use the same quads say when you write in
  • 00:09:21
    ABC as well so of course you will use a
  • 00:09:23
    for loop here you will say for the data
  • 00:09:25
    in F and then every time you run this
  • 00:09:28
    loop you have to say F 1 dot right and
  • 00:09:31
    you will write a data which is data
  • 00:09:32
    right so now it will in ABC you will
  • 00:09:35
    have all that about from my data let's
  • 00:09:37
    run this code go back to ABC and can you
  • 00:09:39
    see that we got all this data here
  • 00:09:41
    awesome right so this is how you can
  • 00:09:42
    work with files so we can use read we
  • 00:09:45
    can use right we can use append now just
  • 00:09:47
    to show you something I have one more
  • 00:09:49
    thing so let me just remove this stuff
  • 00:09:50
    from here so we have a file which is my
  • 00:09:52
    data right now if you can see on left
  • 00:09:54
    hand side we have big as well so there's
  • 00:09:56
    a jpg image if I open that you can see
  • 00:09:58
    it's my photo here so this is of a
  • 00:09:59
    simple photo what our doors I want to
  • 00:10:02
    copy this file or maybe I want to print
  • 00:10:04
    all the data about that file can you
  • 00:10:05
    print data of course right when you talk
  • 00:10:07
    about image it is made up of the binary
  • 00:10:10
    formats wide the pixel intensity all
  • 00:10:12
    those values of course those are numbers
  • 00:10:13
    itself let me try to print the date of
  • 00:10:16
    image let's see how that looks
  • 00:10:17
    so I'll say for I in F I don't know if
  • 00:10:20
    will it work or not let's try let me
  • 00:10:23
    print I so let's see how exactly it
  • 00:10:24
    looks like let's run this code oh we got
  • 00:10:26
    an error
  • 00:10:27
    it says char map okay so cannot decode
  • 00:10:30
    oh that's the issue
  • 00:10:31
    the thing is in files we have two
  • 00:10:34
    different modes one is character mode
  • 00:10:36
    and second is binary mode now when you
  • 00:10:37
    work with file and that file has data
  • 00:10:40
    like actors numbers you can use a
  • 00:10:42
    character format but here you're walking
  • 00:10:44
    with a file wide which is an image and
  • 00:10:46
    when you work with image we don't have
  • 00:10:47
    characters inside image right we have
  • 00:10:49
    numbers we are binary format and that's
  • 00:10:50
    why you need to read this file in a
  • 00:10:52
    binary format now how do you mention
  • 00:10:54
    that it's very easy simply use our B so
  • 00:10:56
    RB means read binary and now let's run
  • 00:11:00
    this code okay see that this is my
  • 00:11:02
    values of the image okay so all these
  • 00:11:04
    are exact code and that's why it is
  • 00:11:06
    printing like there's no hexa values
  • 00:11:07
    yeah so you can see we are putting all
  • 00:11:09
    these values but then it does
  • 00:11:10
    any sense all right this thing doesn't
  • 00:11:12
    make any sense what I will do is I will
  • 00:11:13
    save this data in a particular file
  • 00:11:16
    maybe another image how do I copy this
  • 00:11:17
    image and make another image it's very
  • 00:11:19
    simple let's create a file oh it's a
  • 00:11:22
    file one equal to open and the way we
  • 00:11:25
    have done before but then this time I
  • 00:11:27
    will give a different name I will say my
  • 00:11:29
    hey not my Tita this time my pick dot
  • 00:11:32
    jpg and then we will give a mold so any
  • 00:11:36
    guess of course we have to say right but
  • 00:11:38
    then right binary you have to make sure
  • 00:11:39
    that we like we use right BAM binary
  • 00:11:41
    instead of printing it you will say f1
  • 00:11:43
    dot right and you will write whatever is
  • 00:11:46
    coming from the file one which is upon
  • 00:11:48
    this file let's run this code there is
  • 00:11:50
    no error go back to okay so you contain
  • 00:11:52
    yourself you got my Pig
  • 00:11:53
    open that oh we got a picture okay so
  • 00:11:56
    you can see we have two copies of my
  • 00:11:58
    image this is what amazed the second
  • 00:11:59
    image right so this is how you can work
  • 00:12:01
    with files with characters and Manolis
  • 00:12:04
    let me know in the comment section if
  • 00:12:06
    you are enjoying this series and if you
  • 00:12:07
    have any doubts as well so that's it hit
  • 00:12:09
    that like button and do subscribe for
  • 00:12:11
    for the video bye bye
Tags
  • Python
  • filhåndtering
  • programmering
  • data
  • permanent lagring
  • tekstfiler
  • binære filer
  • open funksjon
  • lese
  • skrive