简体   繁体   中英

Python; read and write all files in a folder

I want to write a code in Python which should read all the files in a folder and then convert them to Wav format. I am using below code, but it get no result:

import glob
import obspy
from obspy import read

directory = '/home/data/*'

files = glob.glob(directory)

for file in files:
    f = obspy.read ('file')
    f.write (str(f.name)+ '.wav', format='wav')

The line f = obspy.read ('file') results in an attempt to read the file named "file". Please mind the difference between the variable file and the string literal "file". It would be good to print various things during the execution. This way you would know what is going on. Try adding print(f.name) after reading the file.

BTW. Are you sure f object has a property name ? I don't see it in the documentation for obspy Stream returned by obspy.read() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM