Python Forum
Copying files if the name is in range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copying files if the name is in range
#1
Greetings!
I'm trying to copy files if the file name has a digit and it is in the range of 0-3.
files in a directory go like this:
file.txt
file-1.txt
file-2.txt
file-3.txt
...
file-50.txt
the snippet I have does not print anything for some reason.
from pathlib import Path
import re

dr ='C:\01'
for ef in Path(dr).iterdir() :
    fn = Path(ef).stem
    #print(f" Names -> {fn}")
    num = re.findall(r'\d+', fn) 
    #print(f" Digit -> {num} - {fn}")
    if num in range(0,3) :
        print(f" File to copy -> {ef}")
Thank you!
Reply
#2
If you un-comment your debug line (9), you should be able to see what's going on: num is a list object and as such, the if at line 10 is never going to return True.

Also, if/when the num list is populated, it's populated with a str object, so there's that also.
tester_V likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
I missed that part! Thank you for pointing!
rob101 likes this post
Reply
#4
It will never find folder C:\01
>>> dr ='C:\01'
>>> dr
'C:\x01'
See the problem?
So like this.
>>> dr = r'C:\01'
>>> dr
'C:\\01'
>>> # Or 
>>> dr = 'C:/01'
>>> dr
>>> 'C:/01'
To show a way with regex.
>>> import re
>>> 
>>> s = 'file-1.txt'
>>> re.search(r'.*[1-3]', s)
<re.Match object; span=(0, 6), match='file-1'>
>>> s = 'file-3.txt'
>>> re.search(r'.*[1-3]', s)
<re.Match object; span=(0, 6), match='file-3'>
>>> s = 'file-5.txt'
>>> re.search(r'.*[1-3]', s)
>>> #No match
Then like this.
from pathlib import Path
import re

dr = r'C:\bar'
for ef in Path(dr).iterdir():
    if re.search(r'.*[0-3]', ef.stem):
        print(ef)
Output:
C:\bar\file-1.txt C:\bar\file-2.txt C:\bar\file-3.txt
tester_V and rob101 like this post
Reply
#5
I still cannot find files in range.

    for enum in num :        
        if enum in range(0,3) :
           print(f" File to copy -> {ef}")
Reply
#6
I got your script to work by altering it a little, to match my system, so you may want to take note of what @snippsat has posted

This is is what I've run:

from pathlib import Path
import re

dr = '/home/rob/Desktop/python/temp'
for ef in Path(dr).iterdir():
    fn = Path(ef).stem
    num = re.findall(r'\d+', fn)
    if num:
        num = int(num[0])
    if num in range(0, 3):
        print(f" File to copy -> {ef}")
Output:
File to copy -> /home/rob/Desktop/python/temp/file-2 File to copy -> /home/rob/Desktop/python/temp/file-1
tester_V likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#7
snippsat, thank you for the snipped!
For some reason, the snippet also prints other files that start with digits "1", "2" or "3"
Like file-33.txt and so on...
Reply
#8
rob101! It works!
You are oficcialy DA MAN now! Wink
Of course "snippsat" is DA BOSS but you are OK too!

Thank you guys! Big Grin
rob101 likes this post
Reply
#9
(Nov-23-2022, 11:09 PM)tester_V Wrote: Like file-33.txt and so on...
Change regex to this.
if re.search(r'.*\b[1-3]\b', ef.stem):
tester_V likes this post
Reply
#10
Well 'snippsat', you are DA MAN too brother... DA MAN! Wink
snippsat likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  matplotlib x axis range goes over the set range Pedroski55 5 3,186 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Copying files from a remote Windows station tester_V 11 6,855 Jul-17-2021, 02:19 AM
Last Post: tester_V
  need to define date range of files to copy over OTH 4 3,113 Aug-07-2020, 12:29 PM
Last Post: buran
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 7,024 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  Copying a file Kundan 1 2,086 Aug-21-2019, 09:55 AM
Last Post: snippsat
  Help copying files with shutil hikerguy62 2 4,292 Aug-02-2019, 08:16 PM
Last Post: hikerguy62
  Copying files from one server to another server folder. naresh 1 2,644 Aug-23-2017, 11:35 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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