0

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
  • Check the error: No such file or directory: 'Common_Tech_Terms' Commented Oct 10, 2019 at 12:05
  • 1
    You might want to edit your heading as well to attract the correct answers. Commented Oct 10, 2019 at 12:08

2 Answers 2

1

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")
Sign up to request clarification or add additional context in comments.

Comments

0

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.