I am very new to Python and I am trying to get a bit of code to concatenate text files into one!
I have the following code:
Checkpoint = open("/Users/owenmurray/Desktop/vocab/Custom_Vocab_Split_by_Brand_Checkpoint.txt", "r")
eightbyeight = open("/Users/owenmurray/Desktop/vocab/Custom_Vocab_Split_by_Brand_8x8.txt", "r")
AmazonAWS = open("/Users/owenmurray/Desktop/vocab/Custom_Vocab_Split_by_Brand_AmazonAWS.txt", "r")
Common_Tech_Terms = open("/Users/owenmurray/Desktop/vocab/Custom_Vocab_Split_by_Brand_Common_Tech_Terms.txt", "r")
import sys
filenames = ['Common_Tech_Terms', 'eightbyeight', 'AmazonAWS', 'Checkpoint']
with open('output_file2.txt', 'w+') as outfile:
for fname in filenames:
with open(fname) as infile:
for line in infile:
outfile.write(line + "\n")
I get the following error:
Traceback (most recent call last):
File "/Users/owenmurray/Desktop/Combining Files", line 60, in <module>
with open(fname) as infile:
IOError: [Errno 2] No such file or directory: 'Common_Tech_Terms'
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "/Users/owenmurray/Desktop/Combining Files"]
[dir: /Users/owenmurray/Desktop]
Anyone have any idea to fix this?
2 Answers 2
You're losing the benefit of using a context manager, call open when you need a file content:
Checkpoint = "/Users/owenmurray/Desktop/vocab/Custom_Vocab_Split_by_Brand_Checkpoint.txt"
eightbyeight = "/Users/owenmurray/Desktop/vocab/Custom_Vocab_Split_by_Brand_8x8.txt"
AmazonAWS = "/Users/owenmurray/Desktop/vocab/Custom_Vocab_Split_by_Brand_AmazonAWS.txt"
Common_Tech_Terms = "/Users/owenmurray/Desktop/vocab/Custom_Vocab_Split_by_Brand_Common_Tech_Terms.txt"
filenames = [Common_Tech_Terms, eightbyeight, AmazonAWS, Checkpoint]
with open('output_file2.txt', 'w+') as outfile:
for fname in filenames:
with open(fname, "r") as infile:
for line in infile:
outfile.write(line + "\n")
answered Oct 10, 2019 at 11:43
ipaleka
3,9772 gold badges16 silver badges39 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
If you do:
Checkpoint = open("/Users/owenmurray/Desktop/vocab/Custom_Vocab_Split_by_Brand_Checkpoint.txt", "r")
Then you will need to manually close the file as well:
Checkpoint.close()
Instead simply do:
# Updated. Removed "open(, 'r')"
Checkpoint = "/Users/owenmurray/Desktop/vocab/Custom_Vocab_Split_by_Brand_Checkpoint.txt"
eightbyeight = "/Users/owenmurray/Desktop/vocab/Custom_Vocab_Split_by_Brand_8x8.txt"
AmazonAWS = "/Users/owenmurray/Desktop/vocab/Custom_Vocab_Split_by_Brand_AmazonAWS.txt"
Common_Tech_Terms = "/Users/owenmurray/Desktop/vocab/Custom_Vocab_Split_by_Brand_Common_Tech_Terms.txt"
import sys
# Removed "". We need variable Common_Tech_Terms, not string "Common_Tech_Terms".
filenames = [Common_Tech_Terms, eightbyeight, AmazonAWS, Checkpoint]
with open('output_file2.txt', 'w+') as outfile:
for fname in filenames:
with open(fname) as infile:
for line in infile:
outfile.write(line + "\n")
answered Oct 10, 2019 at 11:43
Dipen Dadhaniya
4,6602 gold badges19 silver badges26 bronze badges
Comments
lang-py
No such file or directory: 'Common_Tech_Terms'