Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ignore Folder
#1
How to ignore folder in the dir.
It's not a big problem but still, I don't want to rename folder also.

import os

location = input("Enter folder location: ")

def rename(path, name = 'image_0', ext = '.jpg'):
    os.chdir(path)
    x = 1
    for file in os.listdir():
        src = file
        dst = name + str(x) + ext
        os.rename(src, dst)
        x += 1
print("Renamed Successfully")

rename(location)
Reply
#2
Maybe something like:

[f for f in os.listdir(os.curdir) if os.path.isfile(f)]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Not sure what you are trying to do.
Reply
#4
You can use the recommended scandir() method instead of the old listdir() method
import os
 
 
def rename(path, name='image_0', ext='.jpg'):
    # WARNING: UNSAFE: this function may overwrite existing files
    # for example if image_01.jpg already exists in target directory,
    # it may easily be overwritten.
    os.chdir(path)
    x = 1
    for entry in list(os.scandir()):
        if entry.is_dir():
            continue
        dst = '{}{}{}'.format(name, x, ext)
        os.rename(entry.path, dst)
        x += 1

if __name__ ==  '__main__':
    location = input("Enter folder location: ")
    rename(location)        
    print('Renamed Successfully')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Good way to ignore case when searching elements? Winfried 0 36 1 hour ago
Last Post: Winfried
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 552 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Ignore WakeWord after it's said Extra 2 1,189 Apr-01-2022, 12:32 AM
Last Post: Extra
  How to ignore "Retrying (Retry(total=2, connect=2, read=5, redirect=5, status=None))" const 3 2,713 Mar-26-2022, 08:55 AM
Last Post: ndc85430
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,501 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,480 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  Python Cut/Copy paste file from folder to another folder rdDrp 4 5,062 Aug-19-2020, 12:40 PM
Last Post: rdDrp
  Ignore first few letters of a line when reading file. ShakeyPakey 16 6,393 May-30-2020, 02:17 PM
Last Post: BitPythoner
  How to ignore empty columns from DB? Winfried 1 2,285 May-15-2020, 08:35 PM
Last Post: menator01
  How to make the script ignore down devices. wagnergt12 4 3,215 Apr-20-2020, 11:45 PM
Last Post: wagnergt12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020