May I please know how to program this question in python step by step? Thank you
ID: 3713344 • Letter: M
Question
May I please know how to program this question in python step by step? Thank you.
Here is the cats.txt:
Sticky Tape You are tasked with writing a program that will tape files together in a tab separated format. Your program will accept multiple filenames through command line and it is required to open the files. If a file does not exist then then the program should not proceed to stitch any fie contents together and the program should output Unable to load file The program will be ran in this format: python3 stickytape.py You can assume you strictly stitch lines together and spaced by a tab (t If in the event that two files do now have the same amount of lines, an empty string (" should be a placeholder where there is usually an entry Examples: Example 1 Cannot Load File ( python3 stickytape.py file_does_not_exist ) Unable to load file file_does_not_exist Example 2 Two Files(python3 stickytape.py cats.txt dogs.txt): Mainecoon Labrador Siamese BoarderCollie Ginger StaffyExplanation / Answer
# your code goes here
import sys #getting command args
import os.path #checking if file exists
for i in range(len(sys.argv)):
if not os.path.exists(file_path): # check if file exists
print("unable to load file :"+sys.argv[i]) # if not print file unable to load
exit() #exit from program
fp=[] # array of file pointers
maxlines=0; #max number of lines in all files
lines=[]; #array for number of lines in each file
data=[[]] #2d array for storing words from all files
for i in range(len(sys.argv)):
fp[i]=open(sys.argv[i],'r');
lines[i]=sum(1 for line in fp[i]) #count lines
if lines[i]>maxlines: #calculate max lines
maxlines=lines
c=fp[i].read().splitlines(True) #read data
data[i] = [x.split(' ') for x in c] #split into lines
i=0
fn=open('newfile.txt','r+') #open new file for writing
while i<maxlines:
newdate=''
for j in range(len(sys.argv)):
if lines[j]<=i:
newdata=data[j][i]+' ' #combine lines from each file using tab
else:
newdata=' '+' '
fn.write(newdate+' ') #write to file