Question

Python: Please write code to rename all files in the working directory using its last modification...

Python:

Please write code to rename all files in the working directory using its last modification date. Hint: to rename a file, please use os.rename(old_name, new_name)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

Output:

Note: 'ls' list of all files in a current working directory.

Raw Code:

# Importing modules
import os
import datetime
files = os.listdir(".") # List of files in a current working directory.
for file in files: # This loop will iterate through each file in files.
   # finding the last modification date of a file.
   date = datetime.datetime.fromtimestamp(os.path.getmtime(file))
   new_name = str(date.year)+"-"+str(date.month)+"-"+str(date.day) # Year-Month-Day
   os.rename(file,new_name) # Rename a file.

If you have any doubts feel free to comment

Add a comment
Know the answer?
Add Answer to:
Python: Please write code to rename all files in the working directory using its last modification...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program that lists all the files in a directory and their subdirectories, that...

    Write a Java program that lists all the files in a directory and their subdirectories, that mimics the Unix ls command or the Windows dir command. Note that when a directory is encountered we do not immediately print its contents recursively. Rather, as we scan each directory, place any subdirectory in a List. After the directory entries are printed then process each subdirectory recursively. For each file that is listed, include the modification time, the file size, and if it...

  • Python Write the list of all the files in the directory ptest_0 , in a text...

    Python Write the list of all the files in the directory ptest_0 , in a text file contents.txt (this file already exists there.) in separate lines. 1) import glob import shutil import os Beginning_folder = r"/home/MikeyV/Laptop/test_0/learn0_0"; Last_folder = "/home/MikeyV/Laptop/ptest_0"; try:    os.makedirs(last_folder); except;    print"Folder already exists..."; for txt _file in glob.glob(beginning_folder+"\\*txt"):    Shuril.copy2(txt_file,last_folder);

  • Using only shell commands, create a directory structure in your Linux file system to organize files...

    Using only shell commands, create a directory structure in your Linux file system to organize files for this course. An example structure may look like the following: osweb homework homework1.c homework2.java homework3.cpp labs lab1 setup.txt projects Include the following by only using shell commands: List the contents of a directory Write data to a text file Print the contents of a text file to the terminal Delete a text file Move a text file to a different directory Rename a...

  • 1.Write a find command that will display the inode number of all files in the working...

    1.Write a find command that will display the inode number of all files in the working directory (only) that contain the string "#include". 2.Write an ls command with a glob specification that will list the names of all files in the working directory that are exactly 10 characters long, begin with w, x, y, or z, and do not contain a "." character (i.e. no file extension). 3.Write an ls command with a glob specification that will list the names...

  • do numbers 4-8 4. Given any directory, use the Is command to display: • all files...

    do numbers 4-8 4. Given any directory, use the Is command to display: • all files and sub-directories starting with the letter "D" (note do not list anything in any sub-directory) • its immediate sub-directories (sub-directories only, and no other ordinary files) its immediate hidden sub-directories only - take a screenshot (#3-3) that clearly shows the command and the result. 5. Assume that the following files are in the working directory: $ ls intro notesb ref2 section 1 section3 section4b...

  • Write a bash shell script, deleteFilesWithZeroLength.sh, that removes all zero length ordinary files in the directory passed as an optional argument. If you do not specify the directory argument, the script uses the present working directory as the defaul

    Write a bash shell script, deleteFilesWithZeroLength.sh, that removes all zero length ordinary files in the directory passed as an optional argument. If you do not specify the directory argument, the script uses the present working directory as the default argument. Do appropriate exception handling in your script such as:If the arguments are more than 1, print out “Too many arguments passed”.If the argument passed is a regular file, print out “XXX is regular file”.1c. If the directory doesn’t exist, print out “Directory...

  • please write in python. thank you In this question you will be writing the code to...

    please write in python. thank you In this question you will be writing the code to carry out a query on a database table in Python. The CREATE command shown below is given just for your reference, so that you know the structure of the table and its columns. The database is an SQLite database in the file named taxa.sqlite. create table species ( id int primary key, genus varchar ( 5 0 ), species varchar(50), common_name char(100) ) Write...

  • Write a Bash script that removes all zero length ordinary files in the directory (including those...

    Write a Bash script that removes all zero length ordinary files in the directory (including those in the sub-directories at all levels) passed as an optional argument. If you do not specify the directory argument, the script uses the current working directory as the default argument. This problem is for practicing bash programming skills. Though there is an easier way to achieve the goal with find, find is not allowed to appear in your bash script. Post the screenshot of...

  • Problem 4: Write a Bash script that removes all zero length ordinary files in the directory...

    Problem 4: Write a Bash script that removes all zero length ordinary files in the directory (including those in the sub-directories at all levels) passed as an optional argument. If you do not specify the directory argument, the script uses the current working directory as the default argument. This problem is for practicing bash programming skills. Though there is an easier way to achieve the goal with the find command, the find command is not allowed to appear in your...

  • Can you write a Python 2.7 function that takes a directory path, and prints the size,...

    Can you write a Python 2.7 function that takes a directory path, and prints the size, in bytes, of all the files, ignoring sub-directories, reverse sorted by size, with the total in the following exact format: file_name_2: 200000 file_name_1: 100000 file_name_3: 1000 > 3 File(s) - Total: 301000. Provide the copyable code.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT