Skip to content

Commit 2b1cad6

Browse files
committed
Add fillGap
1 parent 3b03a5c commit 2b1cad6

File tree

13 files changed

+83
-3
lines changed

13 files changed

+83
-3
lines changed

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Inspiration 💡
66

7-
Some of the programs in this repository are inspired from the projects given in [Automate the Boring Stuff with Python](https://automatetheboringstuff.com/) by [Al Sweigart](https://github.com/asweigart) and some other are born out of redundant curiosity of a boring mind during lazy afternoons.
7+
Many of the programs in this repository are inspired from the projects given in [Automate the Boring Stuff with Python](https://automatetheboringstuff.com/) by [Al Sweigart](https://github.com/asweigart) and some other are born out of redundant curiosity of a boring mind during lazy afternoons.
88

99
This repository would contain python scripts, some of which might come of use occasionally. The purpose of creating this is to explore the various modules and implementations of the language through creating programs that are as much fun to use as they are to make.
1010

@@ -42,7 +42,8 @@ pip install -r requirements.txt
4242
- Contents
4343
- [Code Destroyer](#Code-Destroyer)
4444
- [Phone Number and Email Extractor](#Phone-Number-and-Email-Extractor)
45-
- [Search files based on Size](#Search-files-based-on-Size)
45+
- [Search files based on size](#Search-files-based-on-size)
46+
- [Fill gaps in naming](#Fill-gaps-in-naming)
4647

4748
- [License](LICENSE)
4849

@@ -74,7 +75,7 @@ python3 phoneAndEmail.py
7475

7576
Two files, `emails.txt` and `phoneNumbers.txt` would be created in the same directory containing the emails and phone numbers from the copied text.
7677

77-
### Search files based on Size
78+
### Search files based on size
7879

7980
Lets you search through a folder based on file size. Asks user for folder path and size. Files and subfolders inside the folder, greater than or equal to the input size would be displayed.
8081

@@ -86,6 +87,19 @@ python3 searchSize.py
8687

8788
When prompted, enter the minimum size (in bytes) and the folder where files are to be searched. If the path entered is correct, the files, above and equal the size entered would be displayed.
8889

90+
### Fill gaps in naming
91+
92+
Finds all files with a given prefix, such as spam001.txt, spam002.txt, and so on, in a single folder and locates any gaps in the numbering (such as if there is a spam001.txt and spam003.txt but no spam002.txt) and reanames all the files to close this gap.
93+
94+
#### Usage:
95+
96+
```py3
97+
python3 fillGap.py
98+
```
99+
100+
Enter the file prefix, extesion name and taret folder in the prompt that appears. The files would be named in
101+
order, closing gaps, if any.
102+
89103
---
90104

91105
File Templates taken from [awesome-bashrc](https://github.com/aashutoshrathi/awesome-bashrc) and [HackerRank-Test-Case-Generator](https://github.com/aashutoshrathi/HackerRank-Test-Case-Generator/).

fillGap/fillGap.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
fillGap.py - A program that finds all files with a given prefix,
3+
such as spam001.txt, spam002.txt, and so on, in a single folder
4+
and locates any gaps in the numbering (such as if there is a
5+
spam001.txt and spam003.txt but no spam002.txt) and reanames all the
6+
files to close this gap.
7+
8+
Usage: run "python3 fillGap.py" and Enter the file prefix, estension
9+
and taret folder in the prompt that appears. The files would be named
10+
in order, closing gaps, if any.
11+
"""
12+
13+
import os
14+
import sys
15+
import re
16+
from math import log
17+
import shutil
18+
19+
# Input file/folder name and extension
20+
filename = input("Enter the name prefix for the files/folders\n")
21+
print("\nEnter the extension after the dot in filenames (like txt in spam01.txt)")
22+
print("Type \"folder\" (without the quotes) if the files are folders")
23+
ext = input()
24+
25+
# Create filename regex
26+
if ext == 'folder':
27+
nameSeq = re.compile(
28+
r'^' + re.escape(filename) + r'\d*' + r'$'
29+
)
30+
else:
31+
nameSeq = re.compile(
32+
r'^' + re.escape(filename) + r'\d*' +
33+
r'[.]' + re.escape(ext) + r'$'
34+
)
35+
36+
# Put matching file/folder names in a list
37+
path = input("\nEnter the path of folder containing the file/folder\n")
38+
path = os.path.abspath(path)
39+
print(path)
40+
if not os.path.isdir(path):
41+
print("The path entered is not a valid folder.")
42+
sys.exit()
43+
else:
44+
lst = []
45+
for entry in os.listdir(path):
46+
found = nameSeq.search(entry)
47+
if found:
48+
lst.append(found[0])
49+
50+
# Sort list and take size of list as padding
51+
lst = sorted(lst)
52+
padding = int(log(len(lst))//log(10)) + 1
53+
54+
# Rename files/folders
55+
for i in range(len(lst)):
56+
if ext == 'folder':
57+
newName = filename + str(i).rjust(padding, '0')
58+
oldPath = os.path.join(path, lst[i])
59+
newPath = os.path.join(path, newName)
60+
shutil.move(oldPath, newPath)
61+
else:
62+
newName = filename + str(i).rjust(padding, '0') + '.' + ext
63+
oldPath = os.path.join(path, lst[i])
64+
newPath = os.path.join(path, newName)
65+
shutil.move(oldPath, newPath)
66+
print("The files are renamed with gaps closed.")

fillGap/test/spam01.txt

Whitespace-only changes.

fillGap/test/spam02.txt

Whitespace-only changes.

fillGap/test/spam04.txt

Whitespace-only changes.

fillGap/test/spam05.txt

Whitespace-only changes.

fillGap/test/spam06.txt

Whitespace-only changes.

fillGap/test/spam07.txt

Whitespace-only changes.

fillGap/test/spam09.txt

Whitespace-only changes.

fillGap/test/spam12.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)