简体   繁体   English

使用 Notepad++ 的 Python 相对路径

[英]Relative path for Python with Notepad++

I am using: Notepad++, Python 3.4, Windows 7我正在使用:记事本++、Python 3.4、Windows 7

I've got the following problem: If I want (for example) to open a file.我有以下问题:如果我想(例如)打开一个文件。 I always have to put in the whole path for example "C:\\Python34\\05_Python_Project\\Python_von_Kopf__\\chapter7\\webapp-chapter7\\cgi-bin\\some_file.txt"我总是必须输入整个路径,例如“C:\\Python34\\05_Python_Project\\Python_von_Kopf__\\chapter7\\webapp-chapter7\\cgi-bin\\some_file.txt”

I want to a write just a short filename like:我只想写一个简短的文件名,例如:

with open ('some_file.txt') as footer_d:
    ...

I realise that Notepad++ is searching in the following path: "C:\\Program Files (x86)\\Notepad++"我意识到 Notepad++ 正在以下路径中搜索:“C:\\Program Files (x86)\\Notepad++”

Can I somehow change/ configure Notepad++ for searching at the file location???我可以以某种方式更改/配置 Notepad++ 以搜索文件位置吗???

A very simple way to implement this, is to do it all in Python:实现这一点的一个非常简单的方法是在 Python 中完成所有操作:

import os

os.chdir("C:/Python34/05_Python_Project/Python_von_Kopf__/chapter7/webapp-chapter7/cgi-bin")

(The Windows API is quite happy with forward slashes as a path separator. It's command line applications that tend not to like them.) Alternatively: (Windows API 非常喜欢使用正斜杠作为路径分隔符。命令行应用程序往往不喜欢它们。)或者:

dirlist = ["C:\\", "Python34", "05_Python_Project", Python_von_Kopf__",
           "chapter7", "webapp-chapter7", "cgi-bin"]
dir = os.path.join(*dirlist)
os.chdir(dir)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM