LaTex – Scripts

Here are some Python-Scripts to manage LaTex Stuff…

CleanUp LaTex Source Code

I had to clean up a folder with Subfolders and LaTex files. To this end I wrote two small Python-Scripts to search for certain types of files and erase them (like backup and hidden files) and to earase the comments from all .tex files. The scripts are so small that they should explain themselves…

#Script to clean up a directory tree!
import os,subprocess

#browse the directory
for dirname, dirnames, filenames in os.walk(‘.’):
for filename in filenames:
#Check every file for:
filepath    = os.path.join(dirname, filename)
#hidden file
if    filename.startswith(‘.’):
print “remove hidden file “+filepath
os.remove(filepath)
#backup file
if    filename.endswith(‘~’):
print “remove backup file “+filepath
os.remove(filepath)
#log file
if    filename.endswith(‘.log’):
print “remove log file “+filepath
os.remove(filepath)

#Script to remove all LaTex-Comments from .tex-files
import os,subprocess

#browse the directory
for dirname, dirnames, filenames in os.walk(‘.’):
for filename in filenames:
#Check every file for:
filepath    = os.path.join(dirname, filename)
#tex file
if    filename.endswith(‘.tex’):
print    “open file: “+filepath
#rename the file
tmpfilepath    = filepath+”~”
process = subprocess.Popen([“mv”,filepath,tmpfilepath])
process.wait()
newfile        = open(filepath,”w”)
oldfile        = open(tmpfilepath,’r’)
for line in oldfile.readlines():
#Search for a comment
pos    = line.find(‘%’)
if    pos != -1:
#Write the line back without the comment
#Sometimes the percent-sign is important. Therefore just erase the comment
newfile.write(line[0:pos+1]+”\n”)
else:
#Write the line completely
newfile.write(line)
newfile.close()
oldfile.close()
process = subprocess.Popen([“rm”,tmpfilepath])
process.wait()

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: