How to display a file’s contents in reversed order?

How to use chdir() function/method form the os module for this.

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?

If the file is huge, many dozens or hundreds of megabytes, you might
not have enough memory to read the entire file into memory at once. In
that case you would need a more complex algorithm that only reads the
file in chunks, reverses each chunk, and prints that.

1 Like

Either that or buy a computer with more memory than storage.

1 Like