Created
March 27, 2018 15:43
-
-
Save JunhongXu/d57d801fa2bbb731c3982cd4c22077ef to your computer and use it in GitHub Desktop.
The simple script helps me clean up unused pictures when writing LATEX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# read .tex file and extract all filenames that have .png, .jpg, .jpeg, and .eps | |
# delete all other files in ./figures/* | |
import re | |
import os | |
import glob | |
SUFFIX = 'png|jpg|jpeg|eps|JPEG|JPG' | |
def extract_filenames(fname): | |
express = re.compile('/figures/(.*.(%s))' % SUFFIX, re.IGNORECASE) | |
results = [] | |
with open(fname) as f: | |
lines = f.readlines() | |
line = lines[0] | |
for line in lines: | |
match = express.findall(line) | |
if len(match) > 0: | |
r = match[0][0].replace('{', '') | |
r = r.replace('}', '') | |
r = 'figures/'+r | |
results.append(r) | |
return results | |
def delete(results): | |
all_files = glob.glob('figures/*') | |
for file in all_files: | |
if file not in results: | |
os.remove(file) | |
results = extract_filenames('thesis_paper_2017.tex') | |
delete(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment