V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yellowtail
V2EX  ›  Python

请问 notebook 中如何获得当前运行文件的名字?

  •  
  •   yellowtail · Nov 2, 2020 · 2327 views
    This topic created in 2001 days ago, the information mentioned may be changed or developed.
    想保存运行结果为运行文件的名字

    或者如何获取文件内某一行的注释?
    2 replies    2020-11-06 20:19:10 +08:00
    abucus
        1
    abucus  
       Nov 6, 2020
    不太理解楼主的问题,如果你是在 notebook 中通过
    %run xxx.py 来运行,那不是已经知道了文件名?

    也许更好的解决方案是在文件里去做保存运行结果的操作,毕竟 py 文件里通过__file__来取得当前文件名更方便

    PS:
    读取某个文件的某行,和一般的读取文件并无不同

    with open('xxx.py','r') as f:

    for line in f:
    yellowtail
        2
    yellowtail  
    OP
       Nov 6, 2020
    @abucus 感谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   846 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 21:21 · PVG 05:21 · LAX 14:21 · JFK 17:21
    ♥ Do have faith in what you're doing.