About 777,000 results
Open links in new tab
  1. python - How to read a file in reverse order? - Stack Overflow

    Feb 20, 2010 · You can also use python module file_read_backwards. After installing it, via pip install file_read_backwards (v1.2.1), you can read the entire file backwards (line-wise) in a memory efficient manner via:

  2. Display the Contents of a Text File in Reverse Order in Python

    Learn how to display the contents of a text file in reverse order using Python with this step-by-step guide.

  3. python - Read lines from a text file, reverse and save in a new …

    Nov 3, 2013 · You want to reverse only the order of lines, you need to use readlines() to get a list of them (as a first approximation, it is equivalent to s = f.read().split('\n')): s = f.readlines() ... f.writelines(s[::-1]) # or f.writelines(reversed(s))

  4. Python Program to Print the Contents of File in Reverse Order

    This is a Python Program to read the contents of a file in reverse order. The program takes a file name from the user and reads the contents of the file in reverse order. 1. Take the file name from the user. 2. Read each line from the file using for loop and store it in a list. 3. Print the elements of list in reverse order. 4. Exit.

  5. Python program to reverse the content of a file and ... - GeeksforGeeks

    Sep 6, 2024 · The task is to reverse as well as stores the content from an input file to an output file. This reversing can be performed in two types. Full reversing: In this type of reversing all the content gets reversed. Word to word reversing: In this kind of reversing the last word comes first and the first word goes to the last position.

  6. Top 5 Efficient Methods to Read a File in Reverse Order

    Nov 6, 2024 · The simplest way to read a file in reverse order is by using Python’s built-in reversed() function in combination with readlines(). This method is easy to implement but be cautious with large files as it loads the entire file into memory.

  7. How to read a text file and output the words in the reversed order ...

    May 24, 2015 · You are already using reversed for the file, you can use it for the lines too; combine it with str.join(): for line in reversed(list(open("text.txt"))): words = line.split() reversed_words = ' '.join(reversed(words)) print(reversed_words)

  8. readline() in Python - GeeksforGeeks

    Apr 21, 2025 · The readline() method in Python is used to read a single line from a file. It is helpful when working with large files, as it reads data line by line instead of loading the entire file into memory. Syntax. file.readline(size) Parameters. size (Optional): The number of bytes from the line to return. Default -1, which means the whole line. Return ...

  9. How to Read from a File in Python - GeeksforGeeks

    Mar 13, 2025 · Reading from a file in Python means accessing and retrieving the contents of a file, whether it be text, binary data or a specific data format like CSV or JSON. Python provides built-in functions and methods for reading a file in python efficiently. Example File: geeks.txt.

  10. How to display a file’s contents in reversed order? - Python Help ...

    Dec 23, 2020 · You can’t use chdir() to display a file in reversed order. To display a file in reversed order, you would: open the file using open. read the file into a string using file.read. reverse the string using slicing string[::-1] print the string. There is no need for chdir in that. What made you think you needed chdir to do this?

Refresh