V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2020-11-02 10:11:43 +08:00 · 1497 次点击
    这是一个创建于 1264 天前的主题,其中的信息可能已经有所发展或是发生改变。
    想保存运行结果为运行文件的名字

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

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

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

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

    for line in f:
    yellowtail
        2
    yellowtail  
    OP
       2020-11-06 20:19:10 +08:00
    @abucus 感谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2206 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 00:36 · PVG 08:36 · LAX 17:36 · JFK 20:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.