Last active
May 22, 2020 21:21
-
-
Save fpaupier/08475906e6bf97035a012a051dbb7b71 to your computer and use it in GitHub Desktop.
Shuffle the paragraphs of a text file with python π
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| # -*- coding: utf8 -*- | |
| import codecs | |
| import random | |
| def shuffle_text_paragraphs(file_path, output_file_path): | |
| """ | |
| Args: | |
| file_path (string): absolute path of the file whose paragraph must be shuffled. | |
| output_file_path (string): absolute path of the file to write the paragraphs in. | |
| Returns: | |
| No output, write directly the shuffled paragraph in the output file. | |
| """ | |
| with codecs.open(file_path, mode='r', encoding='utf-8') as f: | |
| data = f.read() | |
| # Split on \n\n | |
| paragraphs = data.split('\n\n') | |
| # Shuffle splits | |
| random.shuffle(paragraphs) | |
| with codecs.open(output_file_path, mode='w', encoding='utf-8') as output: | |
| for paragraph in paragraphs: | |
| output.write(paragraph) | |
| # Add the line break | |
| output.write('\n\n') | |
| if __name__ == '__main__': | |
| shuffle_text_paragraphs('/path/to/input.txt', '/path/to/output.txt' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should you perform file closing?
f.close()
and
output.close()