×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

deleting all files in a dir

deleting all files in a dir

deleting all files in a dir

(OP)
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

 

Quote:

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

Thanks in advance

RE: deleting all files in a dir

CODE

>>> folder = '\\192.168.0.12\myshare1'
>>> print folder
\192.168.0.12\myshare1
>>> folder = r'\\192.168.0.12\myshare1'
>>> print folder
\\192.168.0.12\myshare1
>>>

RE: deleting all files in a dir

(OP)
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'

RE: deleting all files in a dir

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.
 

RE: deleting all files in a dir

(OP)
Yes I am connected to that share and dir \\192.168.0.12\myshare1 is listin out the files for me.
 

RE: deleting all files in a dir

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

RE: deleting all files in a dir

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close