0

I have a file called names62.txt and it contains 62 names. There is one name per each line, so it contains 62 lines.

I need to develop a Python program that writes the 80% of the names contained in names62.txt to a file called train.txt and 20% of the names to a second output file called test.txt.

How can I do that? These percentages might be changed for future experiments. And I might use different names for txt output files that contains different amount of names (e.g. names128.txt and so on).

starball
  • 20,030
  • 7
  • 43
  • 238
sammyrkhan
  • 45
  • 7

1 Answers1

0

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.

frankfalse
  • 1,553
  • 1
  • 4
  • 17