You can download this code by clicking the button below.
This code is now available for download.
This function shuffles the lines in a specified file and then overwrites the original file.
Technology Stack : os, sys, random
Code Type : File operation
Code Difficulty : Intermediate
import os
import sys
import random
def shuffle_lines(file_path):
"""
Shuffle the lines in a file in place.
"""
with open(file_path, 'r') as file:
lines = file.readlines()
random.shuffle(lines)
with open(file_path, 'w') as file:
file.writelines(lines)