python-os

How to get full path to python program including filename within the program?

强颜欢笑 提交于 2021-02-11 08:58:21
问题 I have a python program, and I want to get the path to the program from within the program, but INCLUDING the file name itself. The name of my file is PyWrapper.py. Right now I'm doing this: import sys,os pathname = os.path.dirname(sys.argv[0]) fullpath = os.path.abspath(pathname) print fullpath The output is: /home/mrpickles/Desktop/HANSTUFF/securesdk/src/ This is the path to the directory in which my file is saved, but I would like it to output: /home/mrpickles/Desktop/HANSTUFF/securesk/src

Better way to find absolute paths during os.walk()?

旧时模样 提交于 2020-08-19 07:21:21
问题 I am practicing with the os module and more specifically os.walk() . I am wondering if there is an easier/more efficient way to find the actual path to a file considering this produces a path that suggests the file is in the original folder when os.walk() is first ran: import os threshold_size = 500 for folder, subfolders, files in os.walk(os.getcwd()): for file in files: filePath = os.path.abspath(file) if os.path.getsize(filePath) >= threshold_size: print filePath, str(os.path.getsize

Can a jupyter notebook find its own filename?

▼魔方 西西 提交于 2020-07-20 07:33:29
问题 Is it possible for a jupyter notebook to get the name of its own file, similarly to what we would do from a python script? os.path.basename(__file__) doesn't seem to work, at least for me on jupyterlab sys.argv[0] returns my_home/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py 回答1: The only way I've found is through JavaScritp as in this answer. The compact form is a cell like this: %%javascript IPython.notebook.kernel.execute(`notebookName = '${window.document.getElementById(

Can a jupyter notebook find its own filename?

断了今生、忘了曾经 提交于 2020-07-20 07:33:07
问题 Is it possible for a jupyter notebook to get the name of its own file, similarly to what we would do from a python script? os.path.basename(__file__) doesn't seem to work, at least for me on jupyterlab sys.argv[0] returns my_home/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py 回答1: The only way I've found is through JavaScritp as in this answer. The compact form is a cell like this: %%javascript IPython.notebook.kernel.execute(`notebookName = '${window.document.getElementById(

os.getenv returns empty output - python

假装没事ソ 提交于 2020-07-10 10:29:29
问题 I am new to python and encountering some issues while executing os commands. I have set my environment variables like as shown below SPARK_HOME = '/opt/spark' HAIL_HOME = '/opt/hail/hail' When I type os.getenv('SPARK_HOME') , I get the below output '/opt/spark/' But when I type os.getenv('HAIL_HOME') , I get blank output Please note that I type the above two commands from a virtual environment using jupyter notebook. Why it works for spark and returns empty for hail Can guide me with this

os.getenv returns empty output - python

▼魔方 西西 提交于 2020-07-10 10:29:01
问题 I am new to python and encountering some issues while executing os commands. I have set my environment variables like as shown below SPARK_HOME = '/opt/spark' HAIL_HOME = '/opt/hail/hail' When I type os.getenv('SPARK_HOME') , I get the below output '/opt/spark/' But when I type os.getenv('HAIL_HOME') , I get blank output Please note that I type the above two commands from a virtual environment using jupyter notebook. Why it works for spark and returns empty for hail Can guide me with this

Unable to get environment variables using os python

回眸只為那壹抹淺笑 提交于 2020-07-03 09:33:40
问题 I recently created two environment variables in my terminal as shown below export SPARK_HOME='/opt/spark/' export HAIL_HOME='/home/ABCD/.pyenv/versions/3.7.2/envs/bio/lib/python3.7/site-packages/hail/' When I use echo $SPARK_HOME or echo $HAIL_HOME , I am able to see the path as output But, when I use the below os commands in jupyter notebook os.getenv('SPARK_HOME') # able to get the output /opt/spark/ os.getenv('HAIL_HOME') # returns no output I also tried defining the same variables from

How do I close a file opened using os.startfile(), Python 3.6

核能气质少年 提交于 2020-06-23 05:09:29
问题 I want to close some files like .txt, .csv, .xlsx that I have opened using os.startfile(). I know this question asked earlier but I did not find any useful script for this. I use windows 10 Environment 回答1: I believe the question wording is a bit misleading - in reality you want to close the app you opend with the os.startfile(file_name) Unfortunately, os.startfile does not give you any handle to the returned process. help(os.startfile) startfile returns as soon as the associated application

Paraview's Python shell not reading file path corectly

余生颓废 提交于 2020-01-25 06:57:10
问题 my main file uses several other files to complete a 3D render in Paraview. I have been using Pythons os library to get the file path that I need. For some reason Paraview breaks when I try to retrieve my path. I am using Paraview's build in shell to run my script. My code goes as follows: import os dir = os.path.dirname(os.path.realpath(__file__)) print(dir) This line of code works inconsistently and I want to understand why this isn't working If my file is in a directory named C:\Test\Test1

How to prevent a race condition when multiple processes attempt to write to and then read from a file at the same time

ε祈祈猫儿з 提交于 2019-12-29 07:48:16
问题 I have the following code (simplified for clarity): import os import errno import imp lib_dir = os.path.expanduser('~/.brian/cython_extensions') module_name = '_cython_magic_5' module_path = os.path.join(lib_dir, module_name + '.so') code = 'some code' have_module = os.path.isfile(module_path) if not have_module: pyx_file = os.path.join(lib_dir, module_name + '.pyx') # THIS IS WHERE EACH PROCESS TRIES TO WRITE TO THE FILE. THE CODE HERE # PREVENTS A RACE CONDITION. try: fd = os.open(pyx_file,