The following script begins with lines 2 and 3 containing no file names -
#! /usr/bin/env python3
#
#
import os.path
no_file_1, no_file_2 = False, False
#open the file containing this script and read, by line, into list 'a', close this file
thisfile = open('__file__','r')
a = []
while True:
file_line = thisfile.readline()
if not file_line:
break
else:
a.append(file_line)
thisfile.close()
#extract the file names from line 2 and 3 of this file (one file name per line)
file_1, file_2 = a [1], a [2]
file_1, file_2 = file_1[1:-1], file_2[1:-1]
#if there are no file(s) listed in line 2 and 3, prompt the user for file name(s)
#and write the file name(s) w/ extenstion '.txt' to a[1] and a[2]
if file_1 == '':
no_file_1 = True
if file_2 == '':
no_file_2 = True
if no_file_1:
file_1 = input('Enter 1st File Name (no extension) >>> ')
a [1] = '#' + file_1 + '.txt' + '\n'
if no_file_2:
file_2 = input('Enter 2nd File Name (no extension) >>> ')
a [2] = '#' + file_2 + '.txt' + '\n'
#... then write a[new script] to this script
if no_file_1 or no_file_2:
thisfile = open(__file__, 'w')
for i in a:
thisfile.write(i)
#now that this script contains file names in lines 2 and 3, check to see if they exist
#in the same directory as this script ... if not, create them.
file_1, file_2 = a [1], a [2]
file_1, file_2 = file_1[1:-1], file_2[1:-1]
if not os.path.exists(file_1):
open(file_1, 'w')
#write to file_1.txt
if not os.path.exists(file_2):
open(file_2, 'w')
thisfile.close()
print(file_1,file_2)
The script is executed, checks for and finds no file names in lines 2 and 3. The user enters two file names and the script overwrites itself with the correct file names, checks to see if the two files exist, if not, the script creates them.
Enter 1st File Name (no extension) >>> file_1
Enter 2nd File Name (no extension) >>> file_2
The next time the script is executed, the file names are defined in lines 2 and 3 and the script checks to see if they exist, if not, the script creates them.
#! /usr/bin/env python3
#file_1.txt
#file_2.txt
import os.path
no_file_1, no_file_2 = False, False
#open the file containing this script and read, by line, into list 'a', close this file
thisfile = open(__file__,'r')
a = []
while True:
file_line = thisfile.readline()
if not file_line:
break
else:
a.append(file_line)
thisfile.close()
#extract the file names from line 2 and 3 of this file (one file name per line)
file_1, file_2 = a [1], a [2]
file_1, file_2 = file_1[1:-1], file_2[1:-1]
#if there are no file(s) listed in line 2 and 3, prompt the user for file name(s)
#and write the file name(s) w/ extenstion '.txt' to a[1] and a[2]
if file_1 == '':
no_file_1 = True
if file_2 == '':
no_file_2 = True
if no_file_1:
file_1 = input('Enter 1st File Name (no extension) >>> ')
a [1] = '#' + file_1 + '.txt' + '\n'
if no_file_2:
file_2 = input('Enter 2nd File Name (no extension) >>> ')
a [2] = '#' + file_2 + '.txt' + '\n'
#... then write a[new script] to this script
if no_file_1 or no_file_2:
thisfile = open(__file__, 'w')
for i in a:
thisfile.write(i)
#now that this script contains file names in lines 2 and 3, check to see if they exist
#in the same directory as this script ... if not, create them.
file_1, file_2 = a [1], a [2]
file_1, file_2 = file_1[1:-1], file_2[1:-1]
if not os.path.exists(file_1):
open(file_1, 'w')
#write to file_1.txt
if not os.path.exists(file_2):
open(file_2, 'w')
thisfile.close()
print(file_1,file_2)