python : how to use wave module? - python-3.x

when I try the following program :
import wave
w = wave.open('f.wav', 'r')
for i in range():
frame = w.readframes(i)
the following error comes :
Traceback (most recent call last):
File "F:/Python31/fg.py", line 2, in <module>
w = wave.open('f.wav', 'r')
File "F:\Python31\lib\wave.py", line 498, in open
return Wave_read(f)
File "F:\Python31\lib\wave.py", line 159, in __init__
f = builtins.open(f, 'rb')
IOError: [Errno 2] No such file or directory: 'f.wav'
can u tell me wat could b the reason ???

The file is not in the path you put that Python interpreter can find. Check that f.wav is in the same path of your script (or chance the path in open).
Is not a wave issue at all.

You are running the python script from a directory where no file f.wav exists. It can't find the file to read. Either copy f.wav to that directory or run you script from the directory f.wav is located in.

Related

file not found error, though file is in proper place

C:\Users\ebena\PycharmProjects\untitled2\venv\Scripts\python.exe C:/Users/ebena/PycharmProjects/untitled2/trial.py
Traceback (most recent call last):
File "C:/Users/ebena/PycharmProjects/untitled2/trial.py", line 5, in <module>
image = Image.open("book background.jpeg")
File "C:\Users\ebena\PycharmProjects\untitled2\venv\lib\site-packages\PIL\Image.py", line 2878, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'book background.jpeg'
python cannot access these files and says they do not exist or file not found
Change the name of the file to book_background.jpeg

Python passing a string value from a table to a function

I'm trying to go through a list of items and passing each one to a function one by one to create an Excel file with the same name as the argument passed. I am getting the error below which I believe is related to the '/' in the String name. Can anyone advise how I get it to ignore this?
>>> test.createExcel(filename)
Traceback (most recent call last):
File "<pyshell#97>", line 1, in <module>
test.createExcel(filename)
File "C:\Users\danie\OneDrive\JVC\project1.py", line 52, in createExcel
wb2.save(modelname+'.xlsx')
File "C:\Users\danie\AppData\Local\Programs\Python\Python37\lib\site-packages\openpyxl\workbook\workbook.py", line 392, in save
save_workbook(self, filename)
File "C:\Users\danie\AppData\Local\Programs\Python\Python37\lib\site-packages\openpyxl\writer\excel.py", line 291, in save_workbook
archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
File "C:\Users\danie\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 1240, in __init__
self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: '14 A4/32GB BLU.xlsx'
A filename cannot contain any of the following characters: \ / : * ? " < > |
In ur caseļ¼Œ u could replace ur filename using str.replace('/','-') or any other character u'd like to.
eg:
wb.save(filename.replace('\','-'))
Or using the regular expression to replace it may work well.

Bad password error while extracting a zip from python zipfile

I am trying a straight forward code:
from zipfile import ZipFile
password = '1sS34nConn3ryTh3B3st007?'
zip_file = 'file.zip'
with ZipFile(zip_file) as zf:
zf.extractall(pwd=bytes(password,'utf-8'))
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib64/python3.6/zipfile.py", line 1524, in extractall
self._extract_member(zipinfo, path, pwd)
File "/usr/lib64/python3.6/zipfile.py", line 1577, in _extract_member
with self.open(member, pwd=pwd) as source, \
File "/usr/lib64/python3.6/zipfile.py", line 1446, in open
raise RuntimeError("Bad password for file %r" % name)
RuntimeError: Bad password for file <ZipInfo filename='file.csv' compress_type=99 file_size=272074 compress_size=60230>
It works perfectly fine when I am extracting it on windows using either 7z or winrar. py7zr also gives error.
I've tried to encrypt a file from 7z Ui on Windows with your password.
After I ran this script:
from zipfile import ZipFile
password = '1sS34nConn3ryTh3B3st007?'
zip_file = 'file.zip'
zf = ZipFile(self.archive_name, 'r')
zf.setpassword(bytes(password,"utf-8"))
zf.extractall(path=".")
No issue. Can you try as well?
You need to use pyzipper module instead of zipfile, because of the newer AES-256 encoding.

paramiko sftp - when trying to download a file, I get OSError File path illegal

Initializing paramiko sftp client:
t = paramiko.Transport((self.sftp_server, 7790))
t.connect(None,self.sftp_user, self.sftp_pw)
sftp = paramiko.SFTPClient.from_transport(t)
listing the files:
dirlist = sftp.listdir(".")
print("Dirlist: %s" % dirlist)
files = sftp.listdir(path=self.location)
print(files)
gives all the files correctly.
but when reading a file form the list
with sftp.open('/VA_EBAC_UP/EBAC_Article_KPI_Report.txt', "r") as f:
data = f.read()
I get an error:
Caught exception: : File path
[EBAC_Article_KPI_Report.txt] illegal.
full stack trace:
Traceback (most recent call last):
File "get_sftp_file_yohan.py", line 234, in get_ftp_files
with sftp.open('/VA_EBAC_UP/EBAC_Article_KPI_Report.txt', "r") as f:
File "/lib/python3.6/site-packages/paramiko/sftp_client.py", line 372, in open
t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
File "/lib/python3.6/site-packages/paramiko/sftp_client.py", line 813, in _request
return self._read_response(num)
File "/lib/python3.6/site-packages/paramiko/sftp_client.py", line 865, in _read_response
self._convert_status(msg)
File "/lib/python3.6/site-packages/paramiko/sftp_client.py", line 898, in _convert_status
raise IOError(text)
OSError: File path [EBAC_Article_KPI_Report.txt] illegal.
this was a Permissions issue. The SFTP server only allowed uploads and I was trying to download from the same server. Maybe this will help someone else - but wish that the error message was verbose enough to highlight permission issue.
**d-wx------** 1 0 0 4096 05 Nov 00:00 VA_EBAC_UP
reading any file from the above folder gives:
Caught exception: : File path [] illegal.

python3 shutil error copying from config list

Im trying to copy a file using shutil by reading in a config.dat file.
This file is simply:
/home/admin/Documents/file1
/home/admin/Documents/file2
Code does work but it will copy file1 okay but then misses file2 because it see a \n there, which im guessing is because of the new line.
#!/usr/bin/python3
import shutil
data = open("config.dat")
filelist = data.read()
src = filelist
dest = '/home/admin/Documents/backup/'
shutil.copy(src, dest)
Error code im getting :
Traceback (most recent call last):
File "./testing.py", line 18, in <module>
shutil.copy(src, dest)
File "/usr/lib/python3.4/shutil.py", line 229, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "/usr/lib/python3.4/shutil.py", line 108, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory:
'/home/admin/Documents/file1\n/home/admin/Documents/file2'
I would like the copy of those files to run based on the files from the config.dat folder but it detects a '\n'. Is there a way to fix this?
thanks for the help
Use split('\n') to get a list of files and the iterate that list. You probably want to throw in the file.strip() to get rid of trailing whitespace and empty lines.
import shutil
dest = '/home/admin/Documents/backup/'
with open('config.dat') as data:
filelist = data.read().split('\n')
for file in filelist:
if file:
shutil.copy(file.strip(), dest)
Or, if you don't need the filelist after this
with open('config.dat') as data:
for file in data:
if file:
shutil.copy(file.strip(), dest)

Resources