Hello!I am trying to write a program in Pynthon.Need help.Requirements:Write a .py file like KevFiles.py (I used Spyder to do this).In that .py, create a function that strips the first letters from all file names in a directory. If the new name is a duplicate of an existing name, Python will throw an exception. Handle that exception by adding “dup” a number as part of the files new name.Call that function from a Jupyter Notebook, passing in the number of characters to delete and the file path. Thank you!

Question
Asked Jun 4, 2019
9 views

Hello!

I am trying to write a program in Pynthon.

Need help.

Requirements:

Write a .py file like KevFiles.py (I used Spyder to do this).
In that .py, create a function that strips the first letters from all file names in a directory. If the new name is a duplicate of an existing name, Python will throw an exception. Handle that exception by adding “dup” a number as part of the files new name.
Call that function from a Jupyter Notebook, passing in the number of characters to delete and the file path.

 

Thank you!

check_circle

Expert Answer

Step 1

Program Plan:

  • Import required packages
  • Prompt the path and the number of characters to delete from the user.
  • Call the method “proc_file” by passing the arguments
  • Define the method “proc_file()”
    • Get the list of file name.
    • Initialize the counter value to “1”.
    • Get the file name from the directory
      • Generate new files
      • Add “dup” and count when the same filename exists
        • Add “dup” with the file name if the condition is “true”.
        • Increment the counter value.
      • Otherwise, append the file name
        • Print the file names.
      • Check for any exception in the file.
        • Print the exception
      • Call the main function
Step 2

File Name: a.py

#Import required package

import os

 

#Main function

def main():

 

    #promt the path of a directory

   print('Enter file directory: ')

   path = input()

 

   #Prompt number of characters to delete

   print('Enter the number of characters to delete? ')

   charDel = int(input())

 

  #Call the method proc_file

   proc_file(charDel, path)

 

#Definition of method proc_file

def proc_file(char_del, path):

 

    #Get the list of file names

   names = list()

   inc = 1

 

   try:

       # Getting file name from the  directory

       for each_file in os.listdir(path):

          

           # Generating new file

           new_fname = each_file[char_del:]

 

           # Adding dup and count when the same filename exists

           if new_fname in names:

               new_fname += '_dup' + str(inc)

               inc += 1

           else:

 

               #Appending the file names

               names.append(new_fname)

 

            #Print the file names

           print(new_fname) 

 

    #Throw the error message

   except Exception as e:

 

       #Print the error message

       print(str(e))

         

#Call the main function

if __name__=='__main__':

   main()

  

 

Step 3

Screenshot #1:

...
fullscreen

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: If you are reading a flat file, with minimal information regarding business rules and constraints. H...

A: Solution:Even though the file contains minimal information about business rules and constraints, the...

question_answer

Q: The symmetric difference of two sets S1 and S2 is defined as S1 ⊝ S2 = {x : x ∈ S1 or x ∈ S2, but x ...

A: Solution:Given that,The symmetric difference between two sets such as “S1” and “S2” can be expressed...

question_answer

Q: What are the internal components of a computer that make it function?

A: A computer system consists of multiple hardware devices which, with the help of software, perform co...

question_answer

Q: Do you think technologies like Big Data and Predictive Analytics in Marketing would have worked 10-1...

A: The technologies like Big Data would not have worked 10-15 years back as due to lack of large datase...

question_answer

Q: how is macro work in word 2016

A: At first we need to enable the developer Tab if it's not enabled previously.Next, we need to click t...

question_answer

Q: When disposing disks or hard drives, it is recommended to format them (writing binary zeros) at leas...

A: Reason of formatting the disks or hard drives is as follows:

question_answer

Q: Consider the following procedure: procedure mystery (x, y, z); integer x, y, z;beginz = 2; y= z+x;en...

A: Pass by reference copies the location of argument to the parameter. Hence, the final value of n, A[1...

question_answer

Q: How would I write a C program to find the length of the longest substring of a given string without ...

A: The given program is to find the maximum length of the substring without repeating character. This i...

question_answer

Q: Typically, a hospital patient receives medication that have been ordered by a particular doctor. Bec...

A: Lets write the business logic first. The business rules between PATIENT and ORDER will be.• A patien...