combining text files in python - python

I am trying to combine multiple text files in a directory in one file. I want to write a HEADER and END statement in the combined file. The current python script which I am using combines all the files into one, but I am not able to figure out how to write a HEADER and END statement for each of the file in the combine file.
filenames = ['pm.pdb.B10010001.txt', 'pm.pdb.B10020001.txt', ...]
with open('/pdb3c91.0/output.txt', 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
for line in infile:
outfile.write(line)

Just write the two lines.
filenames = ['pm.pdb.B10010001.txt', 'pm.pdb.B10020001.txt', ...]
with open('/pdb3c91.0/output.txt', 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
outfile.write("HEADER\n")
for line in infile:
outfile.write(line)
outfile.write("END\n")

Related

Removing comma from Text file using Python

I'm starting to play around with Python and trying to merge a couple of files I have into a single file. When I use the below code:
import glob
path = "C:\\Users\\abc\\OneDrive\\Trading\\"
read_files = glob.glob(path + "*.txt")
with open("result.txt", "wb") as outfile:
for f in read_files:
with open(f, "rb") as infile:
outfile.write(infile.read())
My output file appears to have many names with ,,,, example:
ASX:MCR,,,,,,,
,ASX:RHC,,,,,,
,,ASX:LTR,,,,,
,,,,ASX:MAY,,,
,,,,,,ASX:ANP,
beside it.
How can I remove all the commas to get a list of stock codes in a single line and remove any duplicates:
ASX:BGT
ASX:CNB
ASX:BFG
ASX:ICI

How do I append text to a file with python?

I am trying to make a file that I can continuously add '../' to. My code is as follows:
with open("/tmp/newfile.txt", "a+") as myfile:
myfile.write('../')
contents = myfile.read()
print(contents)
However, when I run this code it returns <empty>
For Append File:
with open("newfile.txt", "a+") as file:
file.write("I am adding in more lines\n")
file.write("And more…")
For Read File:
with open('newfile.txt') as f:
lines = f.readlines()
print(lines)

How to replace a string in every file in a directory

I'm trying to replace with python a string: "XXXXXXXXXXX" with a new string: "ILoveStackOverflow" in every file in a particular folder (all the files in the folder are xml).
My code is as follows:
import os, fnmatch
for filename in os.listdir("C:/Users/Francesco.Borri/Desktop/passivo GME"):
if filename.endswith('.xml'):
with open(os.path.join("C:/Users/Francesco.Borri/Desktop/passivo GME", filename)) as f:
content = f.read()
content = content.replace("XXXXXXXXXXX", "ILoveStackOverflow")
with open(os.path.join("C:/Users/Francesco.Borri/Desktop/passivo GME", filename), mode="w") as f: #Long Pierre-André answer
f.write(content)
The next step would be to replace a different string: "YYYY" with a number that increases every time. If in my directory there are 10 files and I set the starting number 1, the first file "YYYY" will be replaced with 1, the second file with 2 and so on until 10.
You are close. When you open the file the second time, you have to open it in writeable mode to be able to write the content.
with open(os.path.join("C:/Users/Francesco.Borri/Desktop/passivo GME", filename), 'w') as f:
f.write(content)
Once you fix this, I think the second part of your question is just maintaining a variable whose value you increment everytime you replace the string. You could do it manually (iterate over the string), or use the replace function in a for loop:
with open(os.path.join("C:/Users/Francesco.Borri/Desktop/passivo GME", filename)) as f:
content = f.read()
for i in range(content.count("YYYY")):
content.replace("YYYY", str(i), 1) # or str(i+1)
with open(os.path.join("C:/Users/Francesco.Borri/Desktop/passivo GME", filename), 'w') as f:
f.write(content)
with open(os.path.join("C:/Users/Francesco.Borri/Desktop/passivo GME", filename), mode="w") as f:
You must open the file on writing mode.

Concatenating dump files

How to open a dump file (binary)? the answer provided in this question isn't working
filenames = ['file1.dmp', "file2.dmp", "file3.dmp"]
with open('test_file.obj', 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
for line in infile:
outfile.write(line)
file1: 367kb
file2: 1kb
file3: 1000kbp
The output file is only 5kb
When I count lines in the file it returns 4 when I know its much bigger. I think it has to do with the HEX representation which python isn't able to parse?
Hi you are opening the output file with 'w' which won't work mostly for binary files you can open file in wb and then try it.
filenames = ['file1.dmp', "file2.dmp", "file3.dmp"]
with open('test_file.obj', 'wb') as outfile:
for fname in filenames:
with open(fname, 'rb') as infile:
for line in infile:
outfile.write(line)

Script for deleting whitespace for multiple files

I have developed a script which deletes all whitespaces at the end of the file.
import sys
with open("/Users/XXXXX/Desktop/XXXXX.txt") as infile:
lines = infile.read()
while lines.endswith("\n"):
lines = lines[:-2]
with open("/Users/XXXXX/Desktop/XXXXX.txt", 'w') as outfile:
for line in lines:
outfile.write(line)
The script works fine but I have two thousand small files in a folder where I need to delete all whitespaces.
Can someone guide me on how to change my script, so I can open each file in a folder and run the script above ?
thanks,
Try the following code :
import os
import sys
def removeNewLines(file):
with open(file , 'r') as infile:
lines = infile.read()
while lines.endswith("\n"):
lines = lines[:-2]
with open(file, 'w') as outfile:
for line in lines:
outfile.write(line)
all_files = os.listdir('FOLDER PATH')
for file in all_files:
removeNewLines(file)

Categories

Resources