Skip to content

Instantly share code, notes, and snippets.

@fpaupier
Last active May 22, 2020 21:21
Show Gist options
  • Save fpaupier/08475906e6bf97035a012a051dbb7b71 to your computer and use it in GitHub Desktop.
Save fpaupier/08475906e6bf97035a012a051dbb7b71 to your computer and use it in GitHub Desktop.
Shuffle the paragraphs of a text file with python 🐍
#!/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'
@melnarte
Copy link

Should you perform file closing?
f.close()
and
output.close()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment