Python get file regardless of upper or lower

2024/10/12 3:16:56

I'm trying to use this on my program to get an mp3 file regardless of case, and I've this code:

import glob
import fnmatch, redef custom_song(name):for song in re.compile(fnmatch.translate(glob.glob("../music/*"+name+"*.mp3")), re.IGNORECASE):print (song)
custom_song("hello")

But when I execute the script i get the following error:

 File "music.py", line 4, in custom_songfor song in re.compile(fnmatch.translate(glob.glob("../music/*"+name+"*.mp3")), re.IGNORECASE):
TypeError: '_sre.SRE_Pattern' object is not iterable

How can I fix it?

Answer

fnmatch.translate expects a string as an argument, not a list/iterator of filenames as would be returned by glob, thus something like:

pattern = re.compile(fnmatch.translate(name + "*.mp3"), re.IGNORECASE)

Also, you must iterate over some filenames and see if they match the compiled pattern:

directory = '../music/'
for name in os.listdir(directory):if pattern.match(name):print(os.path.join(directory, name))
https://en.xdnf.cn/q/118244.html

Related Q&A

how to save h5py arrays with different sizes?

I am referring this question to this. I am making this new thread because I did not really understand the answer given there and hopefully there is someone who could explain it more to me. Basically my…

Cannot allocate memory on Popen commands

I have a VPS server with Ubuntu 11.10 64bit and sometimes when I execute a subprocess.Popen command I get am getting too much this error:OSError: [Errno 12] Cannot allocate memoryConfig details: For ea…

Python - find where the plot crosses the axhline on python plot

I am doing some analysis on some simple data, and I am trying to plot auto-correlation and partial auto-correlation. Using these plots, I am trying to find the P and Q value to plot in my ARIMA model.I…

remove tick labels in Python but keep gridlines

I have a Python script which is producing a plot consisting of 3 subplots all in 1 column.In the middle subplot, I currently have gridlines, but I want to remove the x axis tick labels.I have triedax2.…

Signal in PySide not emitted when called by a timer

I need to emit a signal periodically. A timer executes certain function, which emits the signal that I want. For some reason this function is not being emitted. I was able to reproduce the error on min…

pybuilder and pytest: cannot import source code when running tests

so i have a project:<root> |- src|-main|-python|-data_merger|- common|- constans|- controller|- resources|- rest|-tests|-unittest|-integrationtestdata_merger is marked as root (I am using Pycharm…

HTTPS proxy server python

I have a problem with my ssl server (in Python). I set the SSL proxy connection in my browser, and try to connect to my ssl server.This is the server:import BaseHTTPServer, SimpleHTTPServer import sslh…

Python 2.7.6 + unicode_literals - UnicodeDecodeError: ascii codec cant decode byte

Im trying to print the following unicode string but Im receiving a UnicodeDecodeError: ascii codec cant decode byte error. Can you please help form this query so it can print the unicode string properl…

Retrieving data from Quandl with Python

How can I get the latest prices from a Quandl dataset with the Python API (https://www.quandl.com/help/python)? On https://www.quandl.com/help/api, it says "You can use rows=n to get only the fir…

Django: Using same object how to show 2 different results in django template?

Using the same object how to SHOW 2 different results using django template ?In one page there are two divs, it should show different information using the same object.INPUTobject data has follows[{&q…