5

I can find my file with this code that I made, but I can't unlink my file as an error shows up that it cant find("my_filename.rxt") can somebody help me with this?

import os
for foldername, subfolders, filenames in os.walk("h:"):
    for subfolder in subfolders:
        for filename in filenames:
            if filename.endswith(".rxt"):
                print(filename)
                os.unlink(filename)

thanks. I was able to do this program with this.

import os

def recursive_unlink(dirname):

for entry in os.scandir(dirname):
    if entry.is_dir():
        recursive_unlink(os.path.abspath(entry))

    elif entry.name.endswith('.rxt'):
        os.unlink(os.path.abspath(entry))

recursive_unlink('h:\desktop')

but when I tried to find a text in the files and delete files with certain texts in them, I couldn't. could someone help me again?

import os

def recursive_unlink(dirname):

    for entry in os.scandir(dirname):
        if entry.is_dir():
            recursive_unlink(os.path.abspath(entry))

        elif entry.name.endswith('.rxt'):
            file = os.path.join(foldername,filename)
            file = open(file, 'r')
            phrase = findWholeWord('MZ・         ク       @                                      コ エ    ヘ!クLヘ!This program cannot be run in DOS mode.')(file)
            if phrase == True
                os.unlink(os.path.abspath(file))

recursive_unlink('h:\\desktop')

PY_NEWBIE
  • 77
  • 4

1 Answers1

3

As @AJS pointed out your problem is that you are not giving absolute path for the file so it cannot find the actual file. Unfortunately I think his current answer for finding absolute path is not right. This should work:

import os

# actually I'm not sure if "h:" is a legal path
for foldername, subfolders, filenames in os.walk("h:"):
    for filename in filenames:
        if filename.endswith('.txt'):
            os.unlink(os.path.join(foldername, filename))

Bonus: os.walk is known to be slow. The alternative method os.scandir can be 20x faster under Windows. So you can also write:

# I think '.rxt' is a typo for '.txt'
def recursive_unlink(dirname):

    for entry in os.scandir(dirname):
       if entry.is_dir():
           recursive_unlink(os.path.abspath(entry))

       elif entry.name.endswith('.txt'):
           os.unlink(os.path.abspath(entry))

recursive_unlink('h:')
knh190
  • 2,744
  • 1
  • 16
  • 30
  • 2
    His way of doing it might not be right, but yours isn't as well. `file = os.path.join(foldername,filename)` should be the way. Your `os.path.abspath()` relates a relative path to the current working directory. – glglgl Apr 10 '19 at 10:01
  • @knh190 actually you may need `os.path.join(foldername,subfolder,filename)` subfolder also, as this will ensure that if there are nested folders the path is constructed correctly – AJS Apr 10 '19 at 10:14
  • @AJS [the doc](https://docs.python.org/3/library/os.html) says you only need `os.path.join(dirpath, name)` for a full path, and `dirpath` is `foldername` here. `subfolder` is at the same level with `filename` – knh190 Apr 10 '19 at 10:17
  • no actually`os.path.join` can have any number of arguments not just two, every argument is later constructed into a complete path. – AJS Apr 10 '19 at 10:19
  • @AJS we are not talking about the same question. See my updated comment please. – knh190 Apr 10 '19 at 10:20
  • yea, my bad misunderstanding on my part. – AJS Apr 10 '19 at 10:34