Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Reading from one file , writing into other file by creating file.
QtWS25 Last Chance

Reading from one file , writing into other file by creating file.

Scheduled Pinned Locked Moved Unsolved Qt for Python
8 Posts 4 Posters 605 Views
  • 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.
  • T Offline
    T Offline
    Teju
    wrote on last edited by
    #1

    How do I read from one file and write into another file by creating that file using python?
    I am able to read file but when I write existing file is getting overwritten. I have set of files containing folder so I have to dynamically create file while writing only. But I am not able to do it. Can someone help me?

    jsulmJ JonBJ 2 Replies Last reply
    0
    • T Teju

      How do I read from one file and write into another file by creating that file using python?
      I am able to read file but when I write existing file is getting overwritten. I have set of files containing folder so I have to dynamically create file while writing only. But I am not able to do it. Can someone help me?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Teju said in Reading from one file , writing into other file by creating file.:

      I am able to read file but when I write existing file is getting overwritten

      So, do you want to append to the existing file, or what?
      Please explain better.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        If you want to append new data to an existing file, open it in append mode.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • T Teju

          How do I read from one file and write into another file by creating that file using python?
          I am able to read file but when I write existing file is getting overwritten. I have set of files containing folder so I have to dynamically create file while writing only. But I am not able to do it. Can someone help me?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Teju said in Reading from one file , writing into other file by creating file.:

          How do I read from one file and write into another file by creating that file using python?
          I am able to read file but when I write existing file is getting overwritten.

          If you want to write to another file do not write to the file you have read from.
          If the other file exists and you tell it to overwrite it, it will overwrite it.
          If the other file exists and you want to append to it, open it for append.

          T 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Teju said in Reading from one file , writing into other file by creating file.:

            I am able to read file but when I write existing file is getting overwritten

            So, do you want to append to the existing file, or what?
            Please explain better.

            T Offline
            T Offline
            Teju
            wrote on last edited by
            #5

            @jsulm as soon as the my task is done I want to create new file, copy all the data from existing file to new file and replace single line.

            1 Reply Last reply
            0
            • JonBJ JonB

              @Teju said in Reading from one file , writing into other file by creating file.:

              How do I read from one file and write into another file by creating that file using python?
              I am able to read file but when I write existing file is getting overwritten.

              If you want to write to another file do not write to the file you have read from.
              If the other file exists and you tell it to overwrite it, it will overwrite it.
              If the other file exists and you want to append to it, open it for append.

              T Offline
              T Offline
              Teju
              wrote on last edited by
              #6

              @JonB i do not understand how to create new file in the middle of the task and copy everything inside existing file to new file by replacing only single line.

              I have to create the file through code,
              Ex if existing file which I am reading is ex.py then new file has to be ex_1.py(having everything exactly same as ex.py except one line)

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by SGaist
                #7
                1. open ex.py in read mode
                2. read ex.py into buffer
                3. close ex.py
                4. modify buffer
                5. open ex_1.py in write mode
                6. write buffer to ex_1.py
                7. close ex_1.py
                8. done

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                T 1 Reply Last reply
                2
                • SGaistS SGaist
                  1. open ex.py in read mode
                  2. read ex.py into buffer
                  3. close ex.py
                  4. modify buffer
                  5. open ex_1.py in write mode
                  6. write buffer to ex_1.py
                  7. close ex_1.py
                  8. done
                  T Offline
                  T Offline
                  Teju
                  wrote on last edited by
                  #8

                  @SGaist thank you I got it.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved