<tfoot id='2DE18'></tfoot><legend id='2DE18'><style id='2DE18'><dir id='2DE18'><q id='2DE18'></q></dir></style></legend>

      1. <i id='2DE18'><tr id='2DE18'><dt id='2DE18'><q id='2DE18'><span id='2DE18'><b id='2DE18'><form id='2DE18'><ins id='2DE18'></ins><ul id='2DE18'></ul><sub id='2DE18'></sub></form><legend id='2DE18'></legend><bdo id='2DE18'><pre id='2DE18'><center id='2DE18'></center></pre></bdo></b><th id='2DE18'></th></span></q></dt></tr></i><div id='2DE18'><tfoot id='2DE18'></tfoot><dl id='2DE18'><fieldset id='2DE18'></fieldset></dl></div>

          <bdo id='2DE18'></bdo><ul id='2DE18'></ul>

        <small id='2DE18'></small><noframes id='2DE18'>

        获取堆栈中上一级函数的 __file__

        Get the __file__ of the function one level up in the stack(获取堆栈中上一级函数的 __file__)
            <legend id='nHCni'><style id='nHCni'><dir id='nHCni'><q id='nHCni'></q></dir></style></legend>
              <tbody id='nHCni'></tbody>

              • <bdo id='nHCni'></bdo><ul id='nHCni'></ul>

              • <small id='nHCni'></small><noframes id='nHCni'>

                <tfoot id='nHCni'></tfoot>

                1. <i id='nHCni'><tr id='nHCni'><dt id='nHCni'><q id='nHCni'><span id='nHCni'><b id='nHCni'><form id='nHCni'><ins id='nHCni'></ins><ul id='nHCni'></ul><sub id='nHCni'></sub></form><legend id='nHCni'></legend><bdo id='nHCni'><pre id='nHCni'><center id='nHCni'></center></pre></bdo></b><th id='nHCni'></th></span></q></dt></tr></i><div id='nHCni'><tfoot id='nHCni'></tfoot><dl id='nHCni'><fieldset id='nHCni'></fieldset></dl></div>

                  本文介绍了获取堆栈中上一级函数的 __file__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我发现我经常使用这种模式:

                  I've found that I'm using this pattern a lot :

                  os.path.join(os.path.dirname(__file__), file_path)
                  

                  所以我决定在一个包含许多这样的小实用程序的文件中放入一个函数:

                  so I've decided to put in a function in a file that has many such small utilities:

                  def filepath_in_cwd(file_path): 
                      return os.path.join(os.path.dirname(__file__), file_path)
                  

                  问题是,__file__ 返回 current 文件,因此返回当前文件夹,我错过了重点.我可以做这个丑陋的 hack(或者继续按原样编写模式):

                  The thing is, __file__ returns the current file and therefore the current folder, and I've missed the whole point. I could do this ugly hack (or just keep writing the pattern as is):

                  def filepath_in_cwd(py_file_name, file_path): 
                      return os.path.join(os.path.dirname(py_file_name), file_path)
                  

                  然后对它的调用将如下所示:

                  and then the call to it will look like this:

                  filepath_in_cwd(__file__, "my_file.txt")
                  

                  但如果我有办法获取堆栈中上一层的函数的 __file__ ,我会更喜欢它.有没有办法做到这一点?

                  but I'd prefer it if I had a way of getting the __file__ of the function that's one level up in the stack. Is there any way of doing this?

                  推荐答案

                  应该这样做:

                  inspect.getfile(sys._getframe(1))
                  

                  sys._getframe(1) 获取调用者框架,inspect.getfile(...) 检索文件名.

                  sys._getframe(1) gets the caller frame, inspect.getfile(...) retrieves the filename.

                  这篇关于获取堆栈中上一级函数的 __file__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)
                  <legend id='WapDR'><style id='WapDR'><dir id='WapDR'><q id='WapDR'></q></dir></style></legend>
                2. <tfoot id='WapDR'></tfoot>

                    <small id='WapDR'></small><noframes id='WapDR'>

                        <tbody id='WapDR'></tbody>
                      • <bdo id='WapDR'></bdo><ul id='WapDR'></ul>

                        <i id='WapDR'><tr id='WapDR'><dt id='WapDR'><q id='WapDR'><span id='WapDR'><b id='WapDR'><form id='WapDR'><ins id='WapDR'></ins><ul id='WapDR'></ul><sub id='WapDR'></sub></form><legend id='WapDR'></legend><bdo id='WapDR'><pre id='WapDR'><center id='WapDR'></center></pre></bdo></b><th id='WapDR'></th></span></q></dt></tr></i><div id='WapDR'><tfoot id='WapDR'></tfoot><dl id='WapDR'><fieldset id='WapDR'></fieldset></dl></div>