5 Replies - 815 Views - Last Post: 12 December 2012 - 05:49 AM Rate Topic: -----

#1 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 10-October 12

how to delete os files that are created in a loop

Posted 18 November 2012 - 07:35 AM

import os
for i in range (100):
   cent=open ( str(i)+'.txt','w')
   cent.close()
   os.system(del str(i)+'txt')




it gives error cause del str(i)+'txt' is not a string..... how can i make this work ?
Is This A Good Question/Topic? 0
  • +

Replies To: how to delete os files that are created in a loop

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,236
  • Joined: 21-June 11

Re: how to delete os files that are created in a loop

Posted 18 November 2012 - 07:51 AM

You're calling the del operator on your string and then you're passing the result (which will be None) to system. I imagine you didn't intent to use the del operator at all, but rather wanted to create a string that starts with "del " followed by the filename.

That said, you probably shouldn't be using system at all to delete files. That's what the os.remove function is for, which has the considerable upside that it works on all operating systems, even ones that don't have a command line utility called del.
Was This Post Helpful? 1
  • +
  • -

#3 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 10-October 12

Re: how to delete os files that are created in a loop

Posted 18 November 2012 - 08:17 AM

View Postsepp2k, on 18 November 2012 - 07:51 AM, said:

You're calling the del operator on your string and then you're passing the result (which will be None) to system. I imagine you didn't intent to use the del operator at all, but rather wanted to create a string that starts with "del " followed by the filename.

That said, you probably shouldn't be using system at all to delete files. That's what the os.remove function is for, which has the considerable upside that it works on all operating systems, even ones that don't have a command line utility called del.

thanks a lot :)
is there any way to achieve deleting all of the files created ( even after the loop) with os.system(del)
using the os.getcwd() and os.listdir() ?
Was This Post Helpful? 0
  • +
  • -

#4 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: how to delete os files that are created in a loop

Posted 27 November 2012 - 10:22 AM

EDIT: Clearly I should drink more coffee.. Sorry for posting in old thread with duped answer.

Why use os.system when you could just use the cross-platform os.remove?


open("Hello.txt", 'w').write("Hello!")
os.remove("Hello.txt")


This post has been edited by Python_4_President: 27 November 2012 - 10:36 AM

Was This Post Helpful? 0
  • +
  • -

#5 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 10-October 12

Re: how to delete os files that are created in a loop

Posted 27 November 2012 - 10:34 AM

View PostPython_4_President, on 27 November 2012 - 10:22 AM, said:

Why use os.system when you could just use the cross-platform os.remove?


open("Hello.txt", 'w').write("Hello!")
os.remove("Hello.txt")


well i just had to do it with os.system for a homework and not with remove.... no matter how stupid it sounds :P
Anyway i figured out myself how to do it ... just for the record it goes like

import os

for i in range (100):

   cent=open ( str(i)+'.txt','w')

   cent.close()
   delete = str('del'+' '+str(i)+'.txt')
   os.system(delete)


Was This Post Helpful? 1
  • +
  • -

#6 eGrove Systems  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 10
  • Joined: 09-December 12

Re: how to delete os files that are created in a loop

Posted 12 December 2012 - 05:49 AM

import os

for i in range (100):

fil=open ( str(i)+'.txt','w')

fil.close() #delete the file through
del_fil = str('del'+' '+str(i)+'.txt')
os.system(del_fil)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1