Python Forum
How do I copy files without case sensitive?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I copy files without case sensitive?
#1
here is my code for copying files from my downloads folder. It only copies one and not all. I'd like it to copy all the readme files so when it looks for the files it's not case sensitive:
from pathlib import Path
import os
import shutil

os.chdir(os.path.abspath(os.path.dirname(__name__)))

def activities():
    homepath = Path('.')
    dir_src = ('~/Downloads')
    for filename in os.listdir(dir_src):
        if filename.startswith('Readme'):
        shutil.copyfile( dir_src + filename, homepath)
    activities()
Think
Reply
#2
if filename.lower().startswith('readme'):
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you, I'll give it a try Big Grin

ok now its not copying any of the 'readme' files to the destination(?
from pathlib import Path
import os
import shutil
os.chdir(os.path.abspath(os.path.dirname(__name__)))
def activities():
    homepath = Path('.')
    dir_src = ('~/Downloads')
    for filename in os.listdir(dir_src):
        if filename.lower().startswith('readme'):
           shutil.copyfile(dir_src + filename, homepath)
    activities()
anything wrong with my code?
Think
Reply
#4
I think you need the full path with the file name as the second argument to copyfile.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
it's homework, so they are going to run it from their computer, so putting my path in there wouldn't work, would it? I thought using path = ('~/Downloads') would be a global(?) type path?
Reply
#6
You'd have to talk to your professor on that one.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
OK i've updated my code to get the users 'homepath' and joined it with the 'source' folder. set my 'destination' folder but stil cant copy the files
from pathlib import Path
import os
import shutil
def activities():
    homepath = os.path.expanduser('~')
    source = os.path.join(homepath, 'Downloads')
    destination = Path('.')
    src_files = os.listdir(source)
    for filename in src_files:
        if filename.startswith('readme'):
            continue
        src = os.path.join(source, filename)
        dst = os.path.join(destination, filename)
        shutil.copyfile(src, dst)
    activities()
Reply
#8
Aren't you trying to copy the readme files? The continue statement on line 11 skips to the top of the for loop, so it's copying everything but the readme files.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
Im trying to find a 'filename' and use case insensitive using fnmatch, glob or filename...ive seen re IGNORECASE but dont know how to use it properly (yet)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy Paste excel files based on the first letters of the file name Viento 2 348 Feb-07-2024, 12:24 PM
Last Post: Viento
  Context-sensitive delimiter ZZTurn 9 1,394 May-16-2023, 07:31 AM
Last Post: Gribouillis
  Create new folders and copy files cocobolli 3 1,336 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Copy only hidden files and folders with rsync Cannondale 2 954 Mar-04-2023, 02:48 PM
Last Post: Cannondale
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 918 Feb-15-2023, 05:34 PM
Last Post: zsousa
  Switch case or match case? Frankduc 9 4,388 Jan-20-2022, 01:56 PM
Last Post: Frankduc
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,391 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Case sensitive checks kam_uk 2 1,968 Nov-30-2020, 01:25 AM
Last Post: bowlofred
  Copy files from subfolders into same name of subfolders at other directory rathoreanil 1 2,306 Oct-12-2020, 01:30 AM
Last Post: Larz60+
  need to define date range of files to copy over OTH 4 3,061 Aug-07-2020, 12:29 PM
Last Post: buran

Forum Jump:

User Panel Messages

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