Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting all files in a dir

Status
Not open for further replies.

blacknred

Technical User
Nov 24, 2010
12
IN
I have a share -//192.168.0.11/myshare which has many files in it.

Which is the best way to delete all the files in this directory using python?

I tried:
Code:
import os 
folder = '\\192.168.0.12\myshare1'


for the_file in os.listdir(folder):
    file_path = os.path.join(folder, the_file)
    try:
        if os.path.isfile(file_path):
            os.unlink(file_path)
    except Exception, e:
        print e
Here, I'm having a hiccup with the slashes

for the_file in os.listdir(folder):
OSError: [Errno 2] No such file or directory: '\\192.168.0.12\\myshare1'

Thanks in advance
 
Code:
>>> folder = '\\192.168.0.12\myshare1'
>>> print folder
\192.168.0.12\myshare1
>>> folder = [COLOR=red]r[/color]'\\192.168.0.12\myshare1'
>>> print folder
[COLOR=red]\[/color]\192.168.0.12\myshare1
>>>
 
yes when i try the print, it shows the path as expected, but when I edit the script as

Code:
import os 
folder = r'\\192.168.0.12\myshare1'


for the_file in os.listdir(folder):
    file_path = os.path.join(folder, the_file)
    try:
        if os.path.isfile(file_path):
            os.unlink(file_path)
    except Exception, e:
        print e

for the_file in os.listdir(folder):
OSError: [Errno 20] Not a directory: '\\\\192.168.0.12\\myshare1'
 
are you even connected to \\192.168.0.12\myshare1 ?
in a command prompt, what happens if you type
dir \\192.168.0.12\myshare1
Code:
C:\WINDOWS>dir \\192.168.0.12\myshare1
The network path was not found.
 
Yes I am connected to that share and dir \\192.168.0.12\myshare1 is listin out the files for me.
 
what if you try "glob" instead of "listdir"? I think "listdir" doesn't include the path in the returned list, whereas, "glob" does.

_________________
Bob Rashkin
 
Are you in a windows or linux environment? shouldn't you be using forward slashes instead of back slashes in the path name, as you have in the 1st line of your O/P, but not in any subsequent lines?

Or does that really have nothing to do with it?

Code what you mean,
and mean what you code!
But by all means post your code!

Razalas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top