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.
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved Qt for Python
8 Posts 4 Posters 678 Views 1 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.
  • T Offline
    T Offline
    Teju
    wrote on 15 Dec 2021, 11:40 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?

    J J 2 Replies Last reply 15 Dec 2021, 11:43
    0
    • T Teju
      15 Dec 2021, 11:40

      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?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 15 Dec 2021, 11:43 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 15 Dec 2021, 14:16
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 15 Dec 2021, 11:58 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
          15 Dec 2021, 11:40

          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?

          J Offline
          J Offline
          JonB
          wrote on 15 Dec 2021, 12:56 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 15 Dec 2021, 14:30
          0
          • J jsulm
            15 Dec 2021, 11:43

            @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 15 Dec 2021, 14:16 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
            • J JonB
              15 Dec 2021, 12:56

              @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 15 Dec 2021, 14:30 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
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 15 Dec 2021, 15:03 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 16 Dec 2021, 18:21
                2
                • S SGaist
                  15 Dec 2021, 15:03
                  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 16 Dec 2021, 18:21 last edited by
                  #8

                  @SGaist thank you I got it.

                  1 Reply Last reply
                  0

                  1/8

                  15 Dec 2021, 11:40

                  • Login

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