Created
November 13, 2018 18:26
-
-
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.
This file contains 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
""" 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