Question
Asked Jun 4, 2019
3 views

Hello!

How to create a Pynthon function that strips the first letters from all file names in a directory?

Thank you

check_circle

Expert Answer

Step 1

Program Instructions:

  • Import the os module and create a function named strip_first_letter().
  • Ask the user for path of the directory.
  • Take each file in the directory using os.lisdir() function and strip the first letter by slicing.
  • Use the os.rename() function to rename the files of the specified directory. The os.rename() function takes path of the source and destination files.
  • call the method to strip the first letter of all file names in the directory.
Step 2

Program:

import os

def strip_first_letter():

  print('Enter full path of a directory? ')

  directory = input()

  try:

    for each_file in os.listdir(directory):

      new_filename = each_file[1:]

      src = directory + "\\" + each_file

      dst = directory + "\\" + new_filename

      os.rename(src, dst)

    print("Done") 

  except Exception as e:

    print(str(e))

 

strip_first_letter()

Step 3

Sample Output:

...
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: I am working on creating a logical entity relationship diagram for my database class. I am wondering...

A: Although the full problem description is not given so deducing the problem / requirements from the d...

question_answer

Q: create a python program that will ask users to input four different manufacturers of cars(e.g Ford, ...

A: Create a class Car and define _init_ () function which will initialize the value of variables manufa...

question_answer

Q: This is C++ my programming lab 11.7: Customer AccountsWrite a program that uses a structure to store...

A: Declare the structure Customer_Accounts with the required data fields. Also, declare an array of 20 ...

question_answer

Q: In PL/SQL, Create a recursive store function called add_numbers to calculate the total of all consec...

A: Program plan:Declare the required variables.The “add_numbers” function returns the value. Otherwise,...

question_answer

Q: Write a program in Eclipse that prompts the user to enter a string and displays the number of vowels...

A: Solution:Given data, The java code gets the input string from user and display the number of vowels ...

question_answer

Q: Develop the ‘Shape’ application such that: ‘Rectangle’, ‘Ellipse’, and ‘Triangle’ classes inherit f...

A: Program plan:Define a class named “Shape”.Define a default constructor to print constructor invoked ...

question_answer

Q: This is a Python question. How do I get the following code to create a numerical list of guest names...

A: We need to convert the guest_name variable as a Python list of 50 elements and initialize each eleme...

question_answer

Q: Convert the grammar S → aSb|Sab|ab into Chomsky normal form.

A: Solution:Chomsky Normal Form (CNF):The Chomsky normal form is a content-free grammar when each and e...

question_answer

Q: Function #1: superman Parameters: height - an integer representing the user's height in inches Descr...

A: Solution:Superman() function:#Define the functiondef superman(input_inches):    #Declare the variabl...