Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Write at the beginn of a File (.py)
Forum Updated to NodeBB v4.3 + New Features

Write at the beginn of a File (.py)

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 6 Posters 3.0k 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.
  • N NotYourFan

    Hey,

    i want to write into a File (Python-File).
    Now i have a Problem:
    When i write somethin into the File it append at the end !
    Follow the Code:

    
        QFile file("C:/Users/Test/Desktop/Projekt/write.py");
        if(file.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text))
        {
            QTextStream stream(&file);
            stream << "test = 'TestFile.csv' ";
        }
    

    i want to write at the top of the File (first or secound line)

    Someone have a Idea why the code writed at the end of the File?

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

    @NotYourFan
    If your intention is to insert some new lines at the start of an existing file while retaining all its subsequent content, you must do it @VRonin's way, i.e. you cannot truly "insert" and move subsequent things downward, you must implement either by reading in the whole existing file and only then starting to rewrite to the file ( @VRonin's code, but don't use that if your file is "large" :) ), or by writing to a separate new file, inserting and then copying from the old file as required.

    1 Reply Last reply
    2
    • N Offline
      N Offline
      NotYourFan
      wrote on last edited by
      #5
      This post is deleted!
      1 Reply Last reply
      0
      • N Offline
        N Offline
        NotYourFan
        wrote on last edited by
        #6

        Oh now i have another problem ...

        at the first four lines are my imports for the script:

        import csv
        import re
        import json
        import math

        i want that to write now "test = 'TestFile.csv'"
        at the fifth line, to looks like this:

        import csv
        import re
        import json
        import math
        
        test = 'TestFile.csv'
        
        jsulmJ J.HilkJ 2 Replies Last reply
        0
        • N NotYourFan

          Oh now i have another problem ...

          at the first four lines are my imports for the script:

          import csv
          import re
          import json
          import math

          i want that to write now "test = 'TestFile.csv'"
          at the fifth line, to looks like this:

          import csv
          import re
          import json
          import math
          
          test = 'TestFile.csv'
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #7

          @NotYourFan Then open the file in Append mode and append empty line and the other one...

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

          1 Reply Last reply
          0
          • N Offline
            N Offline
            NotYourFan
            wrote on last edited by
            #8

            With the Append mode i write my stream at the bottem ... thats the Problem.

            JonBJ 1 Reply Last reply
            0
            • N NotYourFan

              With the Append mode i write my stream at the bottem ... thats the Problem.

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by JonB
              #9

              @NotYourFan
              @jsulm is getting confused with the example you have given. He is assuming the line you want to add is right at the end, but you mean somewhere in the middle, there is still more stuff after it in the file. Just ignore any mentions of "append".

              Look, as I have said, you're probably best here just reading line-by-line from the old file, inserting whatever you want as you go writing it out to a new file. Then delete original and rename new to old at the end. This is always a good idea, as you don't overwrite your existing file with partial stuff if for whatever reason you have an error in your code or something. This is a common way to alter existing files. Just do it this way....

              A completely separate question is why you want to rewrite an existing Python code file anyway? You may have your reasons. But if we took just your example of "injecting" that line into the source, are you sure it could not be achieved better by passing TestFile.csv on the command line at run time as an argument to your Python executable??

              1 Reply Last reply
              1
              • N NotYourFan

                Oh now i have another problem ...

                at the first four lines are my imports for the script:

                import csv
                import re
                import json
                import math

                i want that to write now "test = 'TestFile.csv'"
                at the fifth line, to looks like this:

                import csv
                import re
                import json
                import math
                
                test = 'TestFile.csv'
                
                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #10

                @NotYourFan

                QFile file("C:/Users/Test/Desktop/Projekt/write.py");
                if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
                  QTextStream rstream(&file);
                  QVector<QString> v;
                   while(! rstream.atEnd())
                        v.append(rstream.readLine());
                  file.close();
                  if(file.open(QIODevice::WriteOnly | QIODevice::Text)) {
                    QTextStream wstream(&file);
                
                    for(int i(0); i < v.size(); i++){
                         if( i == 5)
                             wstream <<  "test = 'TestFile.csv' " 
                        wstream << v.at(i);
                
                    }
                    file.close();
                  }
                }
                

                edit: fixed some typos, auto correct can be a pain, sometimes.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                JonBJ 1 Reply Last reply
                3
                • J.HilkJ J.Hilk

                  @NotYourFan

                  QFile file("C:/Users/Test/Desktop/Projekt/write.py");
                  if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
                    QTextStream rstream(&file);
                    QVector<QString> v;
                     while(! rstream.atEnd())
                          v.append(rstream.readLine());
                    file.close();
                    if(file.open(QIODevice::WriteOnly | QIODevice::Text)) {
                      QTextStream wstream(&file);
                  
                      for(int i(0); i < v.size(); i++){
                           if( i == 5)
                               wstream <<  "test = 'TestFile.csv' " 
                          wstream << v.at(i);
                  
                      }
                      file.close();
                    }
                  }
                  

                  edit: fixed some typos, auto correct can be a pain, sometimes.

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #11

                  @J.Hilk
                  I don't wish to start an argument or sound rude, but why encourage to read the whole file into memory when it's just as simple to do it line by line (at least in OP's case) as you go? Plus read-from-old-write-to-new doesn't suffer from blatting the original when the programmer makes a coding error! :) Anyway it's preference for the OP.

                  J.HilkJ 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @J.Hilk
                    I don't wish to start an argument or sound rude, but why encourage to read the whole file into memory when it's just as simple to do it line by line (at least in OP's case) as you go? Plus read-from-old-write-to-new doesn't suffer from blatting the original when the programmer makes a coding error! :) Anyway it's preference for the OP.

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #12

                    @JonB Simple,
                    I took the liberty to copy and past @VRonin example from before and edit it ;-)


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    JonBJ 1 Reply Last reply
                    1
                    • J.HilkJ J.Hilk

                      @JonB Simple,
                      I took the liberty to copy and past @VRonin example from before and edit it ;-)

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #13

                      @J.Hilk
                      Lol! :) OK, I commented on that earlier, far be it for us to disagree with the venerable @VRonin :) Anyway, the OP can do it either way as he sees fit.

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        NotYourFan
                        wrote on last edited by
                        #14

                        @J-Hilk

                        Wehen i run your code, my .py scriptcode changed complete to only ONE Row.

                        J.HilkJ 1 Reply Last reply
                        0
                        • N NotYourFan

                          @J-Hilk

                          Wehen i run your code, my .py scriptcode changed complete to only ONE Row.

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #15

                          @NotYourFan
                          the example is not checked, I don't have a +py file for that.

                          Obviously you need to add a new line character at the end of each line.

                          if( i == 5)
                                       wstream <<  "test = 'TestFile.csv' "  << "\n";
                                  wstream << v.at(i) << "\n";;
                          

                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          1 Reply Last reply
                          2
                          • N Offline
                            N Offline
                            NotYourFan
                            wrote on last edited by
                            #16
                            This post is deleted!
                            1 Reply Last reply
                            0
                            • N Offline
                              N Offline
                              NotYourFan
                              wrote on last edited by
                              #17

                              @J-Hilk
                              now it works the problem is when i changed the wstram it doesent override.

                              i get " test = 'Hallo221.csv' test = 'Hallo1.csv' ...."

                              how i can override?

                              thx

                              JonBJ 1 Reply Last reply
                              0
                              • N NotYourFan

                                @J-Hilk
                                now it works the problem is when i changed the wstram it doesent override.

                                i get " test = 'Hallo221.csv' test = 'Hallo1.csv' ...."

                                how i can override?

                                thx

                                JonBJ Online
                                JonBJ Online
                                JonB
                                wrote on last edited by
                                #18

                                @NotYourFan
                                What does "override" mean? Do you mean "overwrite" what is already in wstream? Do you mean wstream.seek(0)?

                                1 Reply Last reply
                                0
                                • N Offline
                                  N Offline
                                  NotYourFan
                                  wrote on last edited by
                                  #19

                                  yeah sry,

                                  i mean "overwrite".

                                  i always get " test = 'Hallo221.csv' test = 'Hallo1.csv' test = 'Hallo12321.csv' .....

                                  but i want to get only "test = 'Hallo12321.csv' so i want to "overwrite" the line 5 and not add a new string

                                  JonBJ J.HilkJ 2 Replies Last reply
                                  0
                                  • N NotYourFan

                                    yeah sry,

                                    i mean "overwrite".

                                    i always get " test = 'Hallo221.csv' test = 'Hallo1.csv' test = 'Hallo12321.csv' .....

                                    but i want to get only "test = 'Hallo12321.csv' so i want to "overwrite" the line 5 and not add a new string

                                    JonBJ Online
                                    JonBJ Online
                                    JonB
                                    wrote on last edited by JonB
                                    #20

                                    @NotYourFan
                                    So did you try my wstream.seek(0)? after the first write and before the second?
                                    Or, depending on just what you intend by "overwrite", you may need to wstream.resize(0).
                                    Try with both "first-string-is-long", "second-is-short" and then with "first-is-short", "second-string-is-long" to see the difference between seek(0) vs resize(0).

                                    1 Reply Last reply
                                    0
                                    • N NotYourFan

                                      yeah sry,

                                      i mean "overwrite".

                                      i always get " test = 'Hallo221.csv' test = 'Hallo1.csv' test = 'Hallo12321.csv' .....

                                      but i want to get only "test = 'Hallo12321.csv' so i want to "overwrite" the line 5 and not add a new string

                                      J.HilkJ Offline
                                      J.HilkJ Offline
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by J.Hilk
                                      #21

                                      @NotYourFan

                                      You're doing fishy stuff, that may break at any point. 
                                      You should rethink what you're actually trying to do.

                                      for(int i(0); i < v.size(); i++){
                                               if( i == 5) {
                                                   wstream <<  "test = 'TestFile.csv' "   << "\n";
                                                  if(v.at(i).contains("test = '"))
                                                      continue;
                                               }        
                                               wstream << v.at(i)  << "\n";
                                          }
                                      

                                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                      Q: What's that?
                                      A: It's blue light.
                                      Q: What does it do?
                                      A: It turns blue.

                                      N 1 Reply Last reply
                                      3
                                      • J.HilkJ J.Hilk

                                        @NotYourFan

                                        You're doing fishy stuff, that may break at any point. 
                                        You should rethink what you're actually trying to do.

                                        for(int i(0); i < v.size(); i++){
                                                 if( i == 5) {
                                                     wstream <<  "test = 'TestFile.csv' "   << "\n";
                                                    if(v.at(i).contains("test = '"))
                                                        continue;
                                                 }        
                                                 wstream << v.at(i)  << "\n";
                                            }
                                        
                                        N Offline
                                        N Offline
                                        NotYourFan
                                        wrote on last edited by
                                        #22

                                        @J.Hilk
                                        PERFEKT !!!!!
                                        THANK YOU SO MUTCH

                                        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