简体   繁体   中英

Get current directory - 'os' and 'subprocess' library are banned

I'm stuck in a rock and a hard place.

I have been given a very restricted Python 2/3 environment where both os and subprocess library are banned. I am testing file upload functionality using Python selenium; creating the file is straight forward, however, when I use the method driver.send_keys(xyz) , the send_keys is expecting a full path rather than just a file name. Any suggestions on how to work around this?

I do not know if it would work in very restricted Python 2/3 , but you might try following:

create empty file, let say empty.py like so:

with open('empty.py','w') as f:
    pass

then do:

import empty

and:

pathtofile = empty.__file__
print(pathtofile)

This exploits fact empty text file is legal python module (remember to set name which is not use) and that generally import machinery set __file__ dunder, though this is not required, so you might end with None . Keep in mind that if it is not None then it is path to said file ( empty.py ) so you would need to further process it to get catalog itself.

with no way of using the os module it would seem you're SOL unless the placement of your script is static (ie you define the current working dirrectory as a constant) and you then handle all the path/string operations within that directory yourself.

you won't be able to explore what's in the directory but you can keep tabs on any files you create and just store the paths yourself, it will be tedious but there's no reason why it shouldn't work.

you won't be able to delete files i don't think but you should be able to clear their contents

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