Python Merging Files After A Name Fragment
I have two folders when I have dynamic quantity files. For example: In folder "A" I've files:
FileName_1.txt FileName_2.txt FileName_3.txt
In folder "B" I've files:
NewFile_1.txt NewFile_2.txt NewFile_3.txt
The number of files in both folders will always be the same. Is there any simple way how to merge files by number in filename? As result I want to:
Data from file NewFile_1.txt add to FileName_1.txt Data from file NewFile_2.txt add to FileName_2.txt, etc.
It doesn't have to be a solution to the problem. Thank you for the tips.
Just something like this. Grab all the file names from the B directory, find the suffix (after the _), and open the A file based on that:
import os newnames = [k for k in os.listdir( "B") if k[-4:] == '.txt'] for name in newnames: i = name.find('_') suffix = name[i:] olddata = open('B/' + name).read() open('A/FileName'+suffix, 'a').write(olddata)
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module