Skip to content

Instantly share code, notes, and snippets.

@Ojha-Shashikant
Created November 13, 2018 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ojha-Shashikant/c5f545e3a3f20455e0353dbe1eb8210c to your computer and use it in GitHub Desktop.
Save Ojha-Shashikant/c5f545e3a3f20455e0353dbe1eb8210c to your computer and use it in GitHub Desktop.
Taking lines from a line and creating a new file with shuffled lines.
""" Takes a filename as argument
Creates a new file with the name
"shuffle" appended to the original filename.
The lines in the input are shuffled
and written into the new file
"""
import sys
#from s0Cq01_fileops import *
import s0Cq01_fileops
import random
def shuffler(lines):
""" Shuffle a list of strings Input : list of strings
Return : a new list with original strings shuffled
"""
lines[len(lines)-1] = lines[len(lines)-1] + "\n"
new_lines = random.sample(lines, len(lines))
return new_lines
# Get filename from command line
def reading_CLI():
file_name = sys.argv[1]
file_complete_path = "C:\\Users\\Ojha\\Documents\\Python exercises\\Python Scripts\\" + file_name
return file_name, file_complete_path
def new_file_name():
file_name_with_extension = sys.argv[1]
file_name = file_name_with_extension.split(".")[0]
return file_name
# Main starts here
if __name__ == '__main__':
file_name, file_complete_path = reading_CLI()
FH, lines = s0Cq01_fileops.get_lines(file_complete_path)
FH.close()
shuffled_lines = shuffler(lines)
new_name = new_file_name() + "_shuffle.txt"
s0Cq01_fileops.write_lines(new_name, shuffled_lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment