i am currently undergoing my A2 studies in Computer Science and i am having difficulties with random access file processing.
I am trying to have a list UsersArraywhich stores some record data types UsersArray = [lion,soso,Sxia] and loop through the list and store each record in the File TEST.DAT at a specific offset calculated like this Address = hash(UsersArray[i].Password). The problem occurs when i try to do File.seek(Address). It gives me an error and tells me the argument in seek() function is not correct, and i don't understand why this error occurs.
import Users,pickle
File = open("TEST.DAT","rb+")
lion = Users.Users()
lion.Password = "ilovefood"
soso = Users.Users()
soso.Password = "cats123"
Sxia = Users.Users()
Sxia.Password = "luca<3"
UsersArray = [lion,soso,Sxia]
for i in range(3):
Address = hash(UsersArray[i].Password)
File.seek(Address)
pickle.dump(UsersArray[i],File)
File.close()
Error Message:
Traceback (most recent call last):
File "C:\Users\Vaio\Desktop\PythonA2\File Processing\RandomAccessWrite.py", line 17, in <module>
File.seek(Address)
OSError: [Errno 22] Invalid argument
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "C:\Users\Vaio\Desktop\PythonA2\File Processing\RandomAccessWrite.py"]
[dir: C:\Users\Vaio\Desktop\PythonA2\File Processing]
[path: C:\MinGW\bin;C:\Users\Vaio\AppData\Local\Programs\Python\Python36-32\Scripts\;C:\Users\Vaio\AppData\Local\Programs\Python\Python36-32\]
Thank you for the help in advance!
I am inclined to believe that jasonharper nailed the issue. I replicated your code using my own user objects and commented out the pickle.dump() line. I was able to print both the user with the corresponding hash value without any issues. Then I uncommented pickle.dump() and used my own (small) iterative value to use in File.seek(); when I did this, everything worked fine and python wrote to the file. I think the hash values that you're calculating are too large to be written to the file. Not sure if it's part of your assignment or not, but those hash values won't work as file offsets.
Related
C:\Users\skandregula\AppData\Local\Programs\Python\Python37\python.exe
C:/Users/skandregula/Desktop/TestFiles2/testing.py
Traceback (most recent call last):
File "C:/Users/skandregula/Desktop/TestFiles2/testing.py", line 30, in <module>
with open(in_dir + f, 'r') as tmp_file:
FileNotFoundError: [Errno 2] No such file or directory:
'C:/Users/skandregula/Desktop/TestFiles2/history.log.3.3C'
Process finished with exit code 1
that is the error i am running right now...How to fix this?
Without seeing what your code looks like, I'm assuming somewhere in your code you're referencing a file or folder named 'history.log.3.3C' as the error suggests, and that it cannot find it in your directory given. My best advice is make sure the directory is correct?
Hard to tell without seeing what you're trying to do. Maybe posting a snippet of your code would help.
I understand that this question may exist, however I am just trying to figure out my formatting. I am not the best when it comes to formatting things like this and am just doing a bit of guess work that just leads to issues.
I have a custom file type that it must save to, a .gcom and a file name that is declared with the function. Currently the function looks like this:
def cComFile(): # This will NOT write command code (Yet) I want to see if file creation works!
filetype='.gcom'
commands_Directory = 'C:\\Genisis_AI\\Commands\\' # Unused at the moment
file_name = command
if os.path.isfile(("C:\\Genisis_AI\\Commands\\{}").format(file_name).join("{}").format(filetype)):
a = raw_input(('{} already exists! Would you like to overwrite this? [Y/N]: ').format(cComFile.file_name))
string.lower(a)
if a == 'y':
print(("Command: {} , has been rewritten!").format(file_name))
elif a == 'n':
pass
else:
f = open((cComFile.commands_Directory.join(cComFile.file_name.join(cComFile.filetype)),'w+'))
f.write('command was made')
print(('Command made" {}').format(cComFile.command))
I am getting an issue with line 135 which is the following line:
if os.path.isfile(("C:\\Genisis_AI\\Commands\\{}").format(file_name).join("{}").format(filetype)):
The error I am receiving is the following:
Traceback (most recent call last):
File "testing/aaa.py", line 149, in <module>
cComFile('test')
File "testing/aaa.py", line 135, in cComFile
if os.path.isfile(("C:\\Genisis_AI\\Commands\\{}").format(file_name).join("{}").format(filetype)):
KeyError: 'C'
I understand that I am most likely doing this all the wrong way but its a learning experience I suppose. And the chances are that me getting that wrong means that the formatting for the rest of this function are also incorrect as well. Thank you for reading and any help would be nice.
(I understand this is messy and I don't mind it at the moment, I tend to clean up after things work)
You can pass several arguments to format:
if os.path.isfile("C:\\Genisis_AI\\Commands\\{}.{}".format(file_name, filetype)):
Or if you're using Python 3.6 (note the f before the string):
if os.path.isfile(f"C:\\Genisis_AI\\Commands\\{file_name}.{filetype}"):
Or if you prefer the % syntax:
if os.path.isfile("C:\\Genisis_AI\\Commands\\%s.%s" % (file_name, filetype)):
you can pass multiuple values to format function like this, with multiple '{}'.
"C:\\Genisis_AI\\Commands\\{}.{}".format(file_name, filetype)
So basically you jut want to check if a file exists? Why not use a try statement instead of the if?
import errno
try:
with open(filename,'w') as file:
# do stuff with the file
except IOError as e:
# if returned error is [Errno 2] "No such file or directory"
if e.errno == errno.ENOENT:
#do something else
This uses the errno module which makes available error codes from your system. This works for me on windows, may be slightly different on Linux not sure.
I'm running this code in Sublime Text 3 with python 3.6 installed.
The Error I get:
Traceback (most recent call last):
File "C:\SublimeCode\Creating list from txt.pyw", line 3, in <module>
List = open('C:\SublimeCode\Employee\EmployeeList.txt').readlines()
FileNotFoundError: [Errno 2] No such file or directory:
'C:\\SublimeCode\\Employee\\EmployeeList.txt'
I have all these folders as listed in the correct places but yet I get the error.
Code:
List = open('C:\SublimeCode\Employee\EmployeeList.txt').readlines()
print(List)
First using \ is a string literal you must use a \\ to escape this property (or you can use / instead) and you should also set a property as well as read the file before attempting to manipulate the contents.
You can set the property using r (in this case)
list_ = open('C:\\SublimeCode\\Employee\\EmployeeList.txt', 'r').readlines()
print(list_)
You should not use capitals in variables only to set constants. Since list is a function you can escape this property by adding a _ after it see pep8 and as described in this post.
I was following the tutorial on the webpage:
http://pythonhosted.org/bioservices/compound_tutorial.html
Everything worked well until I reached the following command:
uni = UniChem()
and then I received the error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "P:\Anaconda\lib\site-packages\bioservices\unichem.py", line 84, in __init__
maxid_service = int(self.get_all_src_ids()[-1]['src_id'])
TypeError: list indices must be integers, not str
As a minimum working example:
from bioservices import *
uni = UniChem()
and then I receive the error. I understand the error (for the most part) but I don't know how to fix it. So my question is how do I fix the function or work around it?
The overall aim it to map a list of 1000 drug names (and hopefully more in the near future) to Chembl IDs.
The error you saw is probably related to the fact that when you tried to connect to UniChem service, it was off for maintenance or it took too much time to initialize. The consequence is that the service was not started hence the error message you got.
I've just tried (bioservices 1.2.6)
from bioservices import *
uni = UniChem()
and it worked. The following request also worked:
>>> mapping = uni.get_mapping("kegg_ligand", "chembl")
'CHEMBL278315'
This is sort of a follow-up to this question. I want to know if you can access raw devices (i.e. \\.\PhysicalDriveN) in writing mode and if this should be the case, how.
Using Linux, write access can simply be achieved by using e.g. open("/dev/sdd", "w+") (provided that the script is running with root permissions). I assume that Mac OS behaves similar (with /dev/diskN as input file).
When trying the same command under Windows (with the corresponding path), it fails with the following error:
IOError: [Errno 22] invalid mode ('w+') or filename: '\\\\.\\PhysicalDrive3'
However, when trying to read from the PhysicalDrive, it does work (even the correct data is read). The shell is running with administrator permissions under Windows 7.
Is there any other way to accomplish this task using python while still keeping the script as platform-independent as possible?
Edit:
I looked a bit further into what methods python provides for file handling and stumbled across os.open. Opening the PhysicalDrive using os.open(drive_string, os.O_WRONLY|os.O_BINARY) returns no error. So far, so good. Now I have either the choice to write directly to this file-descriptor using os.write, or use os.fdopen to get a file-object and write to it in the regular way.
Sadly, none of these possibilities works. In the first case (os.write()), I get this:
>>> os.write(os.open("\\\\.\\PhysicalDrive3", os.O_WRONLY|os.O_BINARY), "test")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument
In the second case, I can create a file object with write permissions, but the writing itself fails (well, after enforcing its execution using .flush()):
>>> g = os.fdopen(os.open("\\\\.\\PhysicalDrive3", os.O_WRONLY|os.O_BINARY), "wb")
>>> g.write("test")
>>> g.flush()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 22] Invalid argument
As eryksun and agf pointed out in the comments (but I didn't really get it at first), the solution is rather simple: you have to open the device in the rb+ mode, which opens the device for updating (as I have found out now..) without trying to replace it with a new file (which wouldn't work because the file is in fact a physical drive).
When writing, you have to write always a whole sector at a time (i.e. multiples of 512-byte), otherwise it fails.
In addition, the .seek() command can also jump only sector-wise. If you try to seek a position inside a sector (e.g. position 621), the file object will jump to the beginning of the sector where your requested position is (i.e. to the beginning of the second sector, byte 512).
Possibly in Win 7 you have to do something more extreme, such as locking the volume(s) for the disk beforehand with DeviceIoControl(hVol, FSCTL_LOCK_VOLUME, ...)
In Win 7 you don't have to do that; opening and writing with 'rb+' mode works fine.