This answer write the 80% of the lines of file names62.txt
in train.txt
and selects them sequentially from the start of names62.txt
.
Try this code:
train_filename = 'train.txt'
test_filename = 'test.txt'
train = open(train_filename, 'w')
test = open(test_filename, 'w')
p1 = int(input("Insert a number for the percentage? "))
n = 0
with open('names62.txt') as f:
for name in f:
n += 1
num_name_in_train = (n * p1) / 100
n = 0
with open('names62.txt') as f:
for name in f:
n += 1
if n <= num_name_in_train:
train.write(name)
else:
test.write(name)
f.close()
train.close()
test.close()
Input the filenames
I missed to customize the filenames.
To do this you can use the function:
train_filename = input("train filename ? ")
test_filename = input("test filename ? ")
Use of context manager
This post recommends the use of context manager:
It is strongly advised to use a context manager. As an advantage, it is made sure the file is always closed, no matter what:
My previous code uses a context manager only for read the file names62.txt
, while to write to the 2 files train.txt
and test.txt
is used explicit code. That post gives example to write to a file by context manager.