Skip to content
  • Categories
  • Recent
  • Groups
  • Users
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Web Development
  3. can someone give me some tips on how to make Python programs that write data to external files?

can someone give me some tips on how to make Python programs that write data to external files?

Scheduled Pinned Locked Moved Web Development
11 Posts 3 Posters 352 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • szczęśliwyundefined Offline
    szczęśliwyundefined Offline
    szczęśliwy
    Coders Русский(Russian) Classical Pianists Pythons the goat Learning Polish
    wrote on last edited by szczęśliwy
    #1

    so lately i've been learning about making Python programs that write to text files. and I made an example of such (in this we assume in the directory theres a file called "database.txt")

    file = open("database.txt","r+")
    name = input("Enter your name: ")
    age = int(input("Enter your age: "))
    
    def is_minor(age):
        is_minor = False
        if age > 18:
            is_minor = True
        else:
            is_minor = False
        return is_minor
    
    minor = is_minor(age)
    
    def write_to_file(name, age, file, minor):
        file.write(name + ", "
        file.write(str(age) + ", ")
        file.write(minor)
    
    write_to_file(name, age, file, minor)
    file.close()
    

    and now my problem with this is: it does actually work, it writes allow 4 values to the txt file, but everytime it runs it gets rid of what was there before. I don't know if that means that i'm using the wrong file.open("database.txt,"r+"). maybe the "r+" isn't for appending. honestly the problem is likely the file.write statement, so if anyone knows what file.write() statement i could use to append information to the file without overriding the contents, that would be amazing.

    Do not go gentle into that goodnight...

    1 Reply Last reply
    1
    • Linksundefined Offline
      Linksundefined Offline
      Links
      Clown 🤡 Pythons the goat Nintendo Switch Users Eeveelution Fan! Brazilian pepsi lovers meggy lovers NOCTURNAL Smash Ultimate straight allies!
      wrote on last edited by
      #2

      1. Wrong logic in is_minor()

      You wrote:

      if age > 18:
          is_minor = True
      else:
          is_minor = False
      

      This means someone over 18 is considered a minor, which is backwards.
      It should be:

      if age < 18:
          is_minor = True
      else:
          is_minor = False
      

      Or even shorter:

      return age < 18
      

      2. Missing parenthesis in file.write()

      In write_to_file():

      file.write(name + ", "
      

      You forgot the closing ) for that file.write call.


      3. Writing a boolean directly to a file

      file.write() needs a string.
      You’re trying to write:

      file.write(minor)
      

      But minor is a boolean (True or False). You must convert it to string:

      file.write(str(minor))
      

      4. You’re opening the file with r+

      r+ means read/write without truncating. If the file doesn’t exist, it will throw an error. If you just want to append, use "a" or "a+". If you want to overwrite, use "w".


      Fixed Code

      Here’s a working version:

      # Open in append mode
      file = open("database.txt", "a")
      
      name = input("Enter your name: ")
      age = int(input("Enter your age: "))
      
      def is_minor(age):
          return age < 18
      
      minor = is_minor(age)
      
      def write_to_file(name, age, file, minor):
          file.write(name + ", ")
          file.write(str(age) + ", ")
          file.write(str(minor) + "\n")
      
      write_to_file(name, age, file, minor)
      file.close()
      

      ᴺᴼᵂ ᴾᴸᴬᵞᴵᴺᴳ
      [Welcome to Paradise]
      .ılılılllıılılıllllıılılllıllı. - [Green Day]
      ─〇─────
      ↻ ◁ II ▷ ↺

      szczęśliwyundefined 1 Reply Last reply
      1
      • Linksundefined Offline
        Linksundefined Offline
        Links
        Clown 🤡 Pythons the goat Nintendo Switch Users Eeveelution Fan! Brazilian pepsi lovers meggy lovers NOCTURNAL Smash Ultimate straight allies!
        wrote on last edited by
        #3

        Hope it worked :)

        ᴺᴼᵂ ᴾᴸᴬᵞᴵᴺᴳ
        [Welcome to Paradise]
        .ılılılllıılılıllllıılılllıllı. - [Green Day]
        ─〇─────
        ↻ ◁ II ▷ ↺

        1 Reply Last reply
        👍
        1
        • Linksundefined Links

          1. Wrong logic in is_minor()

          You wrote:

          if age > 18:
              is_minor = True
          else:
              is_minor = False
          

          This means someone over 18 is considered a minor, which is backwards.
          It should be:

          if age < 18:
              is_minor = True
          else:
              is_minor = False
          

          Or even shorter:

          return age < 18
          

          2. Missing parenthesis in file.write()

          In write_to_file():

          file.write(name + ", "
          

          You forgot the closing ) for that file.write call.


          3. Writing a boolean directly to a file

          file.write() needs a string.
          You’re trying to write:

          file.write(minor)
          

          But minor is a boolean (True or False). You must convert it to string:

          file.write(str(minor))
          

          4. You’re opening the file with r+

          r+ means read/write without truncating. If the file doesn’t exist, it will throw an error. If you just want to append, use "a" or "a+". If you want to overwrite, use "w".


          Fixed Code

          Here’s a working version:

          # Open in append mode
          file = open("database.txt", "a")
          
          name = input("Enter your name: ")
          age = int(input("Enter your age: "))
          
          def is_minor(age):
              return age < 18
          
          minor = is_minor(age)
          
          def write_to_file(name, age, file, minor):
              file.write(name + ", ")
              file.write(str(age) + ", ")
              file.write(str(minor) + "\n")
          
          write_to_file(name, age, file, minor)
          file.close()
          
          szczęśliwyundefined Offline
          szczęśliwyundefined Offline
          szczęśliwy
          Coders Русский(Russian) Classical Pianists Pythons the goat Learning Polish
          wrote on last edited by
          #4

          @Links Thank you so much! I see what your saying, so I need to fix the is_minor logic, I'm forgetting the ")" in the file.write statement, and I need to converrt the Bool from is_minor into a string. And also, thank you you telling me to open it with "a+", I was very confused with that. Thank you again for your help!

          Do not go gentle into that goodnight...

          Linksundefined 1 Reply Last reply
          1
          • szczęśliwyundefined szczęśliwy

            @Links Thank you so much! I see what your saying, so I need to fix the is_minor logic, I'm forgetting the ")" in the file.write statement, and I need to converrt the Bool from is_minor into a string. And also, thank you you telling me to open it with "a+", I was very confused with that. Thank you again for your help!

            Linksundefined Offline
            Linksundefined Offline
            Links
            Clown 🤡 Pythons the goat Nintendo Switch Users Eeveelution Fan! Brazilian pepsi lovers meggy lovers NOCTURNAL Smash Ultimate straight allies!
            wrote on last edited by
            #5

            @danniltrifonov You're welcome!

            ᴺᴼᵂ ᴾᴸᴬᵞᴵᴺᴳ
            [Welcome to Paradise]
            .ılılılllıılılıllllıılılllıllı. - [Green Day]
            ─〇─────
            ↻ ◁ II ▷ ↺

            1 Reply Last reply
            0
            • Andyboiundefined Offline
              Andyboiundefined Offline
              Andyboi
              Counters
              wrote on last edited by
              #6

              theres always chatgpt 🙏

              Please speed i need this my mamas kinda homless i live with my dad and im just trying to help her. Out 🌹
              https://www.roblox.com/catalog/116549853318185/Kinda-chep-spunh-bop-shirt tuffer shirt
              https://www.roblox.com/catalog/120657443974064 tuff shirt

              Linksundefined 1 Reply Last reply
              0
              • Andyboiundefined Andyboi

                theres always chatgpt 🙏

                Linksundefined Offline
                Linksundefined Offline
                Links
                Clown 🤡 Pythons the goat Nintendo Switch Users Eeveelution Fan! Brazilian pepsi lovers meggy lovers NOCTURNAL Smash Ultimate straight allies!
                wrote on last edited by
                #7

                @Andyboi yeah actually but he sometimes is outdated so its better to just check the forms

                ᴺᴼᵂ ᴾᴸᴬᵞᴵᴺᴳ
                [Welcome to Paradise]
                .ılılılllıılılıllllıılılllıllı. - [Green Day]
                ─〇─────
                ↻ ◁ II ▷ ↺

                1 Reply Last reply
                0
                • szczęśliwyundefined Offline
                  szczęśliwyundefined Offline
                  szczęśliwy
                  Coders Русский(Russian) Classical Pianists Pythons the goat Learning Polish
                  wrote on last edited by
                  #8

                  @Links exactly, plus chatgpt doesn't even work that well on the switch

                  Do not go gentle into that goodnight...

                  Linksundefined 1 Reply Last reply
                  0
                  • szczęśliwyundefined szczęśliwy

                    @Links exactly, plus chatgpt doesn't even work that well on the switch

                    Linksundefined Offline
                    Linksundefined Offline
                    Links
                    Clown 🤡 Pythons the goat Nintendo Switch Users Eeveelution Fan! Brazilian pepsi lovers meggy lovers NOCTURNAL Smash Ultimate straight allies!
                    wrote on last edited by
                    #9

                    @danniltrifonov Yeah and chat gpt cant even write code. it can just correct code

                    ᴺᴼᵂ ᴾᴸᴬᵞᴵᴺᴳ
                    [Welcome to Paradise]
                    .ılılılllıılılıllllıılılllıllı. - [Green Day]
                    ─〇─────
                    ↻ ◁ II ▷ ↺

                    1 Reply Last reply
                    0
                    • Andyboiundefined Offline
                      Andyboiundefined Offline
                      Andyboi
                      Counters
                      wrote on last edited by
                      #10

                      ive heard chatgpt can write code

                      Please speed i need this my mamas kinda homless i live with my dad and im just trying to help her. Out 🌹
                      https://www.roblox.com/catalog/116549853318185/Kinda-chep-spunh-bop-shirt tuffer shirt
                      https://www.roblox.com/catalog/120657443974064 tuff shirt

                      Linksundefined 1 Reply Last reply
                      0
                      • Andyboiundefined Andyboi

                        ive heard chatgpt can write code

                        Linksundefined Offline
                        Linksundefined Offline
                        Links
                        Clown 🤡 Pythons the goat Nintendo Switch Users Eeveelution Fan! Brazilian pepsi lovers meggy lovers NOCTURNAL Smash Ultimate straight allies!
                        wrote on last edited by Links
                        #11

                        @Andyboi it can but it always glitches and gives LOADS OF error messages. (The code)

                        ᴺᴼᵂ ᴾᴸᴬᵞᴵᴺᴳ
                        [Welcome to Paradise]
                        .ılılılllıılılıllllıılılllıllı. - [Green Day]
                        ─〇─────
                        ↻ ◁ II ▷ ↺

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Groups
                        • Users
                        • Tags
                        • Popular