Python Forum
[split] Is there some coding that will do the following in 3 separate events?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Is there some coding that will do the following in 3 separate events?
#21
This will read multiple input files and combine into one output file:

import os

# make sure in same directory as source file (you can modify path as desired)
os.chdir(os.path.abspath(os.path.dirname(__file__)))

input_files = ['firstfile.txt', 'secondfile.txt']


for file in input_files:
    with open(file) as fin, open('thirdfile.txt', "a+") as fout:
        fout.write(fin.read())
test:
firstfile.txt:
Output:
Data from first file
secondfile.txt
Output:
Data from second file.
thirdfile.txt:
Output:
Data from first file Data from second file.
Reply
#22
In line 5 change the w to an a - as in append. That will then write to the end of the second file.
Reply
#23
(Jul-25-2021, 11:25 AM)jefsummers Wrote: In line 5 change the w to an a - as in append. That will then write to the end of the second file.

Thanks @jefsummers - The filename (advert) will remain constant so will be no problem there. However, the output_filename will vary because it will be selected by the user (category).

Is there any way the outfile will "write" upon the "user" opening of that file?
Reply
#24
Sure. The input filename and output filename can be variables
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split Bytearray into separate Files by Hex delimter lastyle 5 2,631 Mar-09-2023, 07:49 AM
Last Post: bowlofred
  Python Split json into separate json based on node value CzarR 1 5,582 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,365 May-17-2022, 07:49 AM
Last Post: Pedroski55
  wait for the first of these events Skaperen 4 1,934 Mar-07-2022, 08:46 PM
Last Post: Gribouillis
  [split] Very basic coding issue aary 4 2,451 Jun-03-2020, 11:59 AM
Last Post: buran
  Simulating events using Hawkes akshit2291 1 2,138 Sep-25-2018, 04:17 AM
Last Post: Larz60+
  win32com Events not catching dageci 0 3,744 Aug-06-2018, 03:18 PM
Last Post: dageci

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020