You can download this code by clicking the button below.
This code is now available for download.
This function is used to shuffle the lines in a specified file randomly and save them back to the original file.
Technology Stack : File operation, random number generation
Code Type : File operation
Code Difficulty : Intermediate
import os
import random
def shuffle_lines(file_path):
with open(file_path, 'r') as file:
lines = file.readlines()
random.shuffle(lines)
with open(file_path, 'w') as file:
file.writelines(lines)
return lines