Last active
April 27, 2021 01:51
-
-
Save ivn951/6cebb611dc3e899fa79ee8e058119d8e to your computer and use it in GitHub Desktop.
Write Files (for Latex) with Iteration & Append strings in list as separate new lines
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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
Created on Mon Apr 26 19:35:55 2021 | |
@author: ivano | |
""" | |
# importing modules | |
import os | |
import subprocess | |
from pathlib import Path | |
# create directory if it doesn't exist | |
Path("schede").mkdir(parents=True, exist_ok=True) | |
# set working directory | |
basdpath = Path('schede/') | |
work_path = os.path.abspath(os.path.dirname(__file__)) | |
os.chdir(os.path.expanduser(work_path+"/schede")) | |
# defines the function for multiline writing | |
def append_multiple_lines(file_name, lines_to_append): | |
# Open the file in append & read mode ('a+') | |
with open(file_name, "a+") as file_object: | |
appendEOL = False | |
# Move read cursor to the start of file. | |
file_object.seek(0) | |
# Check if file is not empty | |
data = file_object.read(100) | |
if len(data) > 0: | |
appendEOL = True | |
# Iterate over each string in the list | |
for line in lines_to_append: | |
# If file is not empty then append '\n' before first line for | |
# other lines always append '\n' before appending line | |
if appendEOL == True: | |
file_object.write("\n") | |
else: | |
appendEOL = True | |
# Append element at the end of file | |
file_object.write(line) | |
# sets the number of files that will be written | |
n = 3 | |
# defines the content in main | |
def main(): | |
for i in range(n): | |
file_name = "S"+str(i).zfill(1)+".tex" | |
subprocess.call(['touch', file_name]) #create file s.tex | |
print (i) # verifico il ciclo | |
list_of_lines = [ | |
'\ecvtitle{}{}', | |
'\ecvitem{\ecvhighlight{Organizzato da:}}{\\textbf{}}', | |
'\ecvitem{\ecvhighlight{}}{}', | |
'\ecvitem{}{}', | |
'\ecvitem{\ecvhighlight{Programma Formativo:}}{}', | |
'\smallskip', | |
'\ecvitem{}{', | |
'\\begin{ecvitemize}' | |
'\item scrivere un\'item', | |
'\\end{ecvitemize}', | |
'}'] | |
# Append strings in list as seperate new lines in the end of file | |
append_multiple_lines(file_name, list_of_lines) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment