IOError: [Errno 22] PyMel/ Python - python-2.7

Good evening SE'ers,
I've got a question that has been bugging me for the last twenty-four hours. I've read up on the issue and my issue seems to be just plain stupid. So, I must be doing something wrong.
FIRST
I'm using xlrd, xlwt and xlutils to create an excel doc and reopen it to check, update and write (save) out over it. Something is causing it to not work correctly and it's apparently only when it saves OVER itself with an updated (copy) workbook.
I got a good piece of information from this "ask", but it doesn't apply to me...
IOError: [Errno 22] invalid mode ('wb') or filename:
SECOND My issue is that I have this as my error:
IOError: [Errno 22] invalid mode ('w+b') or filename: u'D:/LocalData/[username]/Desktop/test1_.xls'
Note, the user name is actually not [username].
The traceback is listed here:
# Error: 22
# Traceback (most recent call last):
# File "<maya console>", line 3, in <module>
# File "D:/LocalData/[username]/Documents/maya/scripts\ExportExcel.py", line 144, in main
# writeExcel()
# File "D:/LocalData/[username]/Documents/maya/scripts\ExportExcel.py", line 107, in writeExcel
# writeInt(wb,wsInt,filePath,detectName,fileName)
# File "D:/LocalData/[username]/Documents/maya/scripts\ExportExcel.py", line 48, in writeInt
# logTheMatList(wb,wsInt,filePath,detectName)
# File "D:/LocalData/[username]/Documents/maya/scripts\ExportExcel.py", line 35, in logTheMatList
# wb.save(filePath+'MaterialList_'+str(detectName)+'.xls')
# File "D:\Program Files\Autodesk\Maya2013\Python\lib\xlwt\Workbook.py", line 696, in save
# doc.save(filename_or_stream, self.get_biff_data())
# File "D:\Program Files\Autodesk\Maya2013\Python\lib\xlwt\CompoundDoc.py", line 262, in save
# f = open(file_name_or_filelike_obj, 'w+b')
EDIT1:
Figured out that I cannot open a file that and overwrite it. I'm not sure why, but if it's the same name and file location, it highly disapproves and provides an error. Does anyone have any suggestions as to how to avoid this obvious issue?

Figured it out on my own.
If you are opening an excel doc to read from and save over while utilizing os, xlrd, xlwt and xlutils... You must follow these instructions (which probably could have been solved by someone who knew less than me ;) ).
import all the important, necessary modules
rb=xlrd.open_workbook([your location here]+[your file name]+'.xls') WARNING do not use on_demand=True, this keeps your file open
os.remove([your location here]+[your file name]+'.xls')
wb=copy(rb)
wb.save([your location here]+[your file name]+'.xls')
This is the most effective way to update a file you created (like a typical save in a program) without "deleting" your file, which, you will be but will be saving a newer copy.

Related

Pyglet - TypeError: expected string or buffer

I'm trying to use pyglet instead of pygame, 'cause it supports several screens.
this is a sample code that I run:
import pyglet
display = pyglet.canvas.get_display()
screens = display.get_screens()
window = pyglet.window.Window(fullscreen=True, screen=screens[1])
pyglet.app.run()
and I get this error:
Traceback (most recent call last): File
"/home/pi/netcomShopTV/idk.py", line 5, in
window = pyglet.window.Window() File "/usr/local/lib/python2.7/dist-packages/pyglet/init.py", line 359,
in getattr
import(import_name) File "/usr/local/lib/python2.7/dist-packages/pyglet/window/init.py",
line 1890, in
gl._create_shadow_window() File "/usr/local/lib/python2.7/dist-packages/pyglet/gl/init.py", line
209, in _create_shadow_window
_shadow_window = Window(width=1, height=1, visible=False) File "/usr/local/lib/python2.7/dist-packages/pyglet/window/xlib/init.py",
line 171, in init
super(XlibWindow, self).init(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/pyglet/window/init.py",
line 642, in init
self._create() File "/usr/local/lib/python2.7/dist-packages/pyglet/window/xlib/init.py",
line 265, in _create
self.context.set_vsync(self._vsync) # XXX ? File "/usr/local/lib/python2.7/dist-packages/pyglet/gl/xlib.py", line 265,
in set_vsync
warnings.warn(e) TypeError: expected string or buffer
Pyglet Version: 1.4.8
I searched in internet, couldn't find anything to solve this problem.
It seems this bug was introduced with this recent change. You should definitely raise it on pyglet github issue tracker.
Meanwhile, try installing the version prior to 1.4.8. (I though suspect this may just lead to crashing on failed sync as opposed to trying to warn you and then crashing :)).
As #alecxe mentioned, it was a bug. After I opened a ticket on github, I got the solution:
This is an exception for a Raspberry Pi specific issue. It's supposed
to raise a warning, and pass without crashing. If possible, could you
try editing line 265 in
/usr/local/lib/python2.7/dist-packages/pyglet/gl/xlib.py, and
changing:
warnings.warn(e) to warnings.warn(e.message)

IOError: [Errno 22] invalid mode ('rb') or filename

I am a beginner at python and trying to use a very simple shutil module (shutil.copy) to copy databases from multiple folders into a backup folder. I am getting the error below. Any help is appreciated.
# importing os module
import os
#import time module
import time
import datetime
# importing shutil module
import shutil
now = datetime.datetime.now()
timestamp = str(now.strftime("%Y%m%d_%H%M%S"))
source5 = "F:/SHARED/SOP/PRE GO LIVE/TEST CASES & SCENARIOS/MASTER/PRE_GO_LIVE_MASTER.accdb"
dest5 = "F:/SHARED/SOP/SB/Python/Destination/PRE_GO_LIVE_MASTER.accdb_"+timestamp+".accdb"
print("Before copying ")
DB5 = shutil.copy(source5,dest5)
print("After DATABASE has been copied")
Error:
Traceback (most recent call last):
File "C:\Users\sbasava1\Desktop\Python\Final_Attempt.py", line 101, in <module>
DB5 = shutil.copy(source5,dest5)
File "C:\Python27\lib\shutil.py", line 119, in copy
copyfile(src, dst)
File "C:\Python27\lib\shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 22] invalid mode ('rb') or filename:
Check your file path, use double backslashes \\ or a single forward slash / or make your string a raw string r"...".
# Original path - wouldn't work
path = "c:\location\directory"
# Raw string - would work
path = r"c:\location\directory"
# Double slashes - would work
path = "c:\\location\\directory"
# Forward slashes - would work
path = "c:/location/directory"
Consider learning about string literals
If this didn't help leave me a comment, it would also help to see the/part of the code you are working with!
Edit: Running your script didn't give me an issue:
Check if the directory you are trying to create a file in actually exists

UTF-8 error with the python open() function

I wrote a simple code in python the only opens and read a file
def read_text():
quotes = open("‪C:/Users/Matteo/Desktop/quotes.txt")
contents_of_file = quotes.read()
print(contents_of_file)
quotes.close()
read_text()
When i try to execute it this is what appears
Traceback (most recent call last):
File "C:\Python27\read.py", line 6, in <module>
read_text()
File "C:\Python27\read.py", line 2, in read_text
quotes = open("‪C:/Users/Matteo/Desktop/quotes.txt")
IOError: [Errno 22] invalid mode ('r') or filename: '\xe2\x80\xaaC:/Users /Matteo/Desktop/quotes.txt'
Searching on the internet i understood that the problem is that IDLE recognizes an Unicode character before C, \xe2\x80\xaa, that is a "LEFT-TO-RIGHT EMBEDDING". I have no idea of what is this and how to remove from my code.
Your code contains an invisible character (probably because you copy/pasted the filename from somewhere). Try deleting the "C: part and retyping it.

Python pdfkit can't find wkhtmltopdf executable

a few months ago I had created a little script to convert URLs to PDF files, this morning I tried to convert another link but I got this error from py prompt:
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\pdfkit\api.py", line 24, in from_url
configuration=configuration, cover_first=cover_first)
File "C:\Python27\lib\site-packages\pdfkit\pdfkit.py", line 42, in __init__
self.configuration = (Configuration() if configuration is None
File "C:\Python27\lib\site-packages\pdfkit\configuration.py", line 27, in __init__
'https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf' % self.wkhtmltopdf)
IOError: No wkhtmltopdf executable found: ""
If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf -
https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf
I've googled a lot but still can't find solution. Anyone can help me?
I found the solution, I don't know why but python was replacing my environment path "C:\Program Files\wkhtmltopdf\bin" with "C:\Program Files\wkhtmltopdin" because of "\b" so I just replaced "\b" with "/b" and solved.

Unable to copy files of certain extension in Python using loop

I want to loop through the files present in Audio_files folder and check if it is raw file or wave file and put it in the respective directories.
Here is my code:
import shutil
import os
source = os.listdir("/home/GM/codec_implement/Audio_files/")
destination = "/home/GM/codec_implement/raw_files/"
destination2 = "/home/GM/codec_implement/raw_files/wave_files/"
for files in source:
if files.endswith(".wav"):
shutil.copy(files,destination)
elif files.endswith(".raw"):
shutil.copy(files,destination)
else:
print "Invalid format"
Please let me know where I am making mistake. Error i get is:
Traceback (most recent call last): File "./checkaudiofiles.py", line
12, in shutil.copy(files,destination) File
"/usr/lib/python2.7/shutil.py", line 119, in copy copyfile(src, dst)
File "/usr/lib/python2.7/shutil.py", line 82, in copyfile with
open(src, 'rb') as fsrc: IOError: [Errno 2] No such file or directory:
'test_sound.wav'
The error means that the test_sound.wav file is not on that directory. These file might be removed between the two proces or the os.listdir() and the shutil.copy() don't list the files the same way.