-4
data = data2 = data3 = ""

with open('pushkin.txt', encoding='utf-8', mode='r') as fp:
    data = fp.read()

with open('romeo.txt', encoding='utf-8', mode='r') as fp:
    data2 = fp.read()
with open('byron.txt', encoding='utf-8', mode='r') as fp:
    data3 = fp.read()


data += "\n"
data += data2 + data3

with open('poems.txt', 'w', encoding='utf-8') as fp:
    fp.write(data)

With this code, I have transferred three text files into one. Now I need to turn over text in that file, like this:

ABC       CDA
BCA  -->  BCA
CDA       ABC

but I don't know how. I thought using reverse() or reversed(), but that makes it just from abc to cba

Teriyaki
  • 29
  • 4
  • 2
    Welcome to Stack Overflow. To be clear: you want the **lines of** the file to be reversed? Did you try using a reading method that would separate the lines of the file first? Did you try reading the documentation to look for something like that? Does that solve the problem for you? – Karl Knechtel Jul 05 '22 at 06:20
  • I really tried looking for it, but no, it didn't help me. – Teriyaki Jul 05 '22 at 06:22
  • How do the ABC etc relate to the content. Is each a separate file, so that you want to reverse the sequence of concatenation? Is each a word or a line and logically unrelated to its source file? How would you describe in words the desired algorithm? – MisterMiyagi Jul 05 '22 at 06:23
  • "I really tried looking for it, but no, it didn't help me." What happens if you try putting `python read lines of a file` [into a search engine](https://duckduckgo.com/?q=python+read+lines+of+a+file)? – Karl Knechtel Jul 05 '22 at 06:27
  • Note that even if you got an answer, please [edit] the question to make it clear what the requirements are. Otherwise, the question is not useful for future readers that need to figure out if their task matches yours. – MisterMiyagi Jul 05 '22 at 06:31

1 Answers1

1

You can use reverse() to reverse the order of elements in a list after saving all the contents as list type to data variable, not the specific string, as follows:

data = []

with open('1.txt', encoding='utf-8', mode='r') as fp: # ABC
    data += fp.readlines()
    # data -> ['ABC\n']

with open('2.txt', encoding='utf-8', mode='r') as fp: # BCA
    data += fp.readlines()
    # data -> ['ABC\n', 'BCA\n']

with open('3.txt', encoding='utf-8', mode='r') as fp: # CDA
    data += fp.readlines()
    # data -> ['ABC\n', 'BCA\n', 'CDA\n']

data.reverse() # data becomes ['CDA\n', 'BCA\n', 'ABC\n']

with open('result.txt', 'w', encoding='utf-8') as fp:
    fp.write("".join(data))
# result.txt
CDA
BCA
ABC
Park
  • 2,446
  • 1
  • 16
  • 25
  • 1
    Hello Park! This code worked for me, big thanks to you, gonna accept the answer in 4 minutes. – Teriyaki Jul 05 '22 at 06:27
  • Heads up that this reverses the order of the lines, not (just) the order of the files. If each file contains more than a single line, this may or may not be desired. – MisterMiyagi Jul 05 '22 at 06:29
  • @Clownpiece Pleased to help you. Just be cautiuous when you use `reverse()` for `list` and `str` ;) – Park Jul 05 '22 at 06:30
  • @MisterMiyagi Oh, you're right. I corrected the point. Thx – Park Jul 05 '22 at 06:34