Python Forum

Full Version: i need to change the code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import random
nazwaPlikuZDanymi = open('123.txt')
content = nazwaPlikuZDanymi.read()
allObjects = content.splitlines()
sizeTST = len(allObjects)/5


tst = random.sample(allObjects, k=int(sizeTST))

for x in range(len(tst)):
        allObjects.remove(tst[x])

theFile = open('tst1.txt', 'w')
for item in tst:
  theFile.write("%s\n" % item)
theFile.close()

theFile = open('trn1.txt', 'w')
for item in allObjects:
  theFile.write("%s\n" % item)
theFile.close()

tmp = tst
tst = random.sample(allObjects, k=int(sizeTST))

for x in range(len(tst)):
        allObjects.remove(tst[x])

theFile = open('tst2.txt', 'w')
for item in tst:
  theFile.write("%s\n" % item)
theFile.close()

theFile = open('trn2.txt', 'w')
for item in allObjects:
  theFile.write("%s\n" % item)
for item in tmp:
  theFile.write("%s\n" % item)
theFile.close()

tmp = tst
tst = random.sample(allObjects, k=int(sizeTST))

for x in range(len(tst)):
        allObjects.remove(tst[x])

theFile = open('tst3.txt', 'w')
for item in tst:
  theFile.write("%s\n" % item)
theFile.close()

theFile = open('trn3.txt', 'w')
for item in allObjects:
  theFile.write("%s\n" % item)
for item in tmp:
  theFile.write("%s\n" % item)
theFile.close()

tmp = tst
tst = random.sample(allObjects, k=int(sizeTST))

for x in range(len(tst)):
        allObjects.remove(tst[x])

theFile = open('tst4.txt', 'w')
for item in tst:
  theFile.write("%s\n" % item)
theFile.close()

theFile = open('trn4.txt', 'w')
for item in allObjects:
  theFile.write("%s\n" % item)
for item in tmp:
  theFile.write("%s\n" % item)
theFile.close()

tmp = tst
tst = random.sample(allObjects, k=int(sizeTST))

for x in range(len(tst)):
        allObjects.remove(tst[x])

theFile = open('tst5.txt', 'w')
for item in tst:
  theFile.write("%s\n" % item)
theFile.close()

theFile = open('trn5.txt', 'w')
for item in allObjects:
  theFile.write("%s\n" % item)
for item in tmp:
  theFile.write("%s\n" % item)
theFile.close()
can u help me guys? i need to change this code but have to work like this. I mean diffrent function or something
i dont know python and i have to do this on tomorrow ;(
Do not double post the same question.

You have not provided enough information to help you.  Additionally, if you "don't know python" and aren't willing to learn, this isn't the right place.  We help people solve their problems who show effort to learn.  We do not do your work for you.
On top of Mekire's comments - you haven't said what change you actually need in the code.
(Nov-22-2017, 09:07 PM)woiwoi Wrote: [ -> ]i have to do this on tomorrow

Probably should have started earlier, tbh, especially if you don't know anything about what it is you need to do.