Working with Dates and Times |Section 6|Celestial Warrior

1 year ago
1

Here is a tricky exercise.

1.Please download thethree text files attached in this lecture and put them in a folder. The first text file contains the textContent1.The second contains Content2and the third Content3.

2. You should create a Python script that generates a new text file which should contain the content of all three text files. So the generated file should have this content:

Content1
Content2
Content3

In other words, your Pythonscript should merge the three text files.

3. Also, the name of the output file should be the current timestamp. Example:2017-11-01-13-57-39-170965.txt

You have some tips in the next lecture and the solution in the lecture afterthat.

1. Consider using the glob2 third-party library to generate a list of filenames to iterate through.

2. Use a "with" statement to create a new text file and then iterate through the file list inside that "with"statement and open andread existing file contentsin each iterationand write them to new text file.

import glob2
import datetime

filenames=glob2.glob("*.txt")

with open(datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S-%f")+".txt", 'w') as file:
for filename in filenames:
with open(filename,"r") as f:
file.write(f.read()+"\n")

Loading comments...