我正在构建一个简单的帮助脚本,用于将代码库中的几个模板文件复制到当前目录.但是,我没有存储模板的目录的绝对路径.我确实有一个来自脚本的相对路径,但是当我调用脚本时,它将其视为相对于当前工作目录的路径.有没有办法指定这个相对url来自脚本的位置?
在我的路上,我有一点问题.
这个代码示例创建了一些名为"〜/ some_dir"的目录,并且不明白我想在我的主目录中创建some_dir.
my_dir = "~/some_dir"
if not os.path.exists(my_dir):
os.makedirs(my_dir)
Run Code Online (Sandbox Code Playgroud)
请注意,这是基于Linux的系统.
假设python代码在先前的Windows目录中称为"main"而不知道,并且在运行时安装代码的任何地方都需要访问目录"main/2091/data.txt".
我应该如何使用开放(位置)功能?应该是什么位置?
编辑:
我发现下面的简单代码会起作用..它有什么缺点吗?
file="\2091\sample.txt"
path=os.getcwd()+file
fp=open(path,'r+');
Run Code Online (Sandbox Code Playgroud) 我有这个代码:
allFiles = os.listdir(myPath)
for module in allFiles:
if 'Module' in module: #if the word module is in the filename
dirToScreens = os.path.join(myPath, module)
allSreens = os.listdir(dirToScreens)
Run Code Online (Sandbox Code Playgroud)
现在,一切正常,我只需要更改线路
allSreens = os.listdir(dirToScreens)
Run Code Online (Sandbox Code Playgroud)
获取只是文件的列表,而不是文件夹.因此,当我使用
allScreens [ f for f in os.listdir(dirToScreens) if os.isfile(join(dirToScreens, f)) ]
Run Code Online (Sandbox Code Playgroud)
它说
module object has no attribute isfile
Run Code Online (Sandbox Code Playgroud)
注意:我使用的是Python 2.7
我有这样的目录结构
projectfolder/fold1/fold2/fold3/script.py
Run Code Online (Sandbox Code Playgroud)
现在我给script.py一个路径作为文件的命令行参数
fold1/fold_temp/myfile.txt
Run Code Online (Sandbox Code Playgroud)
所以基本上我希望能够以这种方式提供路径
../../fold_temp/myfile.txt
>>python somepath/pythonfile.py -input ../../fold_temp/myfile.txt
Run Code Online (Sandbox Code Playgroud)
这里的问题是我可能会获得完整路径或相对路径,因此我应该能够决定并基于此我应该能够创建绝对路径.
我已经掌握了与路径相关的功能.
参考问题给出了部分答案,但我不知道如何使用其中提供的功能构建完整路径.
试图在我的驱动器上的目录中钻取,其中包含子分支.当我找到具有我正在寻找的文件扩展名的文件时,我想要完整的文件路径.现在这就是我所拥有的:
import os
import Tkinter
import tkFileDialog
from Tkinter import Tk
from tkFileDialog import askopenfilename
root = Tkinter.Tk().withdraw()
dirname = tkFileDialog.askdirectory(initialdir='.')
list = []
for root, dirs, files in os.walk(dirname):
for name in files:
if name.find(".txt") != -1:
name = str(name)
name = os.path.realpath(name)
list.append(name)
print list
Run Code Online (Sandbox Code Playgroud)
这是返回的
c:\users\name\desktop\project\file.txt
Run Code Online (Sandbox Code Playgroud)
但是file.txt位于
c:\users\name\desktop\project\folder1\file.txt
Run Code Online (Sandbox Code Playgroud) 在遵循符号链接的同时,有许多解决文件路径的示例,我想在不解决符号链接的情况下获得文件的绝对路径。我认为这是解决方案:
ex=Path('example.txt')
abs_ex = ex.parent.resolve()/ex
Run Code Online (Sandbox Code Playgroud)
在其他函数或参数方面我缺少resolve()
什么吗?
关于使用os.path.abspath,有一个答案,但我想了解Python 3.6或更高版本中的pathlib。
我可以使用自己编写的代码找到文件,但是由于出现错误提示无法找到文件(“ my_filename.rxt”),我无法解除链接,因此有人可以帮助我吗?
import os
for foldername, subfolders, filenames in os.walk("h:"):
for subfolder in subfolders:
for filename in filenames:
if filename.endswith(".rxt"):
print(filename)
os.unlink(filename)
thanks. I was able to do this program with this.
Run Code Online (Sandbox Code Playgroud)
导入操作系统
def recursive_unlink(dirname):
for entry in os.scandir(dirname):
if entry.is_dir():
recursive_unlink(os.path.abspath(entry))
elif entry.name.endswith('.rxt'):
os.unlink(os.path.abspath(entry))
Run Code Online (Sandbox Code Playgroud)
recursive_unlink('h:\ desktop')
但是,当我尝试在文件中查找文本并删除其中包含某些文本的文件时,我做不到。有人可以再次帮助我吗?
import os
def recursive_unlink(dirname):
for entry in os.scandir(dirname):
if entry.is_dir():
recursive_unlink(os.path.abspath(entry))
elif entry.name.endswith('.rxt'):
file = os.path.join(foldername,filename)
file = open(file, 'r')
phrase = findWholeWord('MZ? ?? ? @ ? ? ?!?L?!This program cannot be …
Run Code Online (Sandbox Code Playgroud) 我有一个文件路径和一个目录路径。该文件应该位于该目录中的某个位置(多个级别)。我想将文件路径的开头与目录路径进行比较。所以我基本上做的是:
if file_path.startswith(directory_path):
do_something()
Run Code Online (Sandbox Code Playgroud)
两条路径都是字符串。不幸的是,我的文件路径包括“..”和“.”。所以它看起来像这样:/home/user/documents/folder/../pictures/house.jpg
。由于另一条路径不包含这些点,因此比较显然会失败。python 有没有办法从字符串中删除这些点?我想过使用 os 模块中的 path.join() ,但这不起作用。非常感谢您的帮助:)
current_working_directory = os.getcwd()
relative_directory_of_interest = os.path.join(current_working_directory,"../code/framework/zwave/metadata/")
files = [f for f in os.listdir(relative_directory_of_interest) if os.path.isfile(os.path.join(relative_directory_of_interest,f))]
for f in files:
print(os.path.join(relative_directory_of_interest,f))
Run Code Online (Sandbox Code Playgroud)
输出:
/Users/2Gig/Development/feature_dev/scripts/../code/framework/zwave/metadata/bar.xml
/Users/2Gig/Development/feature_dev/scripts/../code/framework/zwave/metadata/baz.xml
/Users/2Gig/Development/feature_dev/scripts/../code/framework/zwave/metadata/foo.xml
Run Code Online (Sandbox Code Playgroud)
这些文件路径工作正常,但我想看到这些是绝对路径(没有任何../ - 我不知道为什么我关心,也许我是强迫症.我想我也觉得在登录时更容易看到标准python库(我在3.2)中是否有内置的东西可以将它们变成绝对路径?如果不是,那就没什么大不了的,但是一点点谷歌搜索只发现了第三方解决方案.
编辑:看起来像我想要的os.path.abspath(文件))所以上面的修改后的源码现在看起来像(不是我没有测试这个,它只是袖口):
current_working_directory = os.getcwd()
relative_directory_of_interest = os.path.join(current_working_directory,"../code/framework/zwave/metadata/")
files = [f for f in os.listdir(relative_directory_of_interest) if os.path.isfile(os.path.join(relative_directory_of_interest,f))]
for f in files:
print(os.path.abspath(f))
Run Code Online (Sandbox Code Playgroud)
输出:
/ Users/2Gig/Development/feature_dev/scripts/ZWave_custom_cmd_classes(08262010).xml/Users/2Gig/Development/feature_dev/scripts/ZWave_custom_cmd_classes(09262010).xml/Users/2Gig/Development/feature_dev/scripts/ZWave_custom_cmd_classes(09262011). XML
我下载了一种名为 redline.ttf 的字体,我想在 pygame 中使用它。我只想在屏幕上打印文本。我在文件夹 pygame -> lib 中找到了一种名为“freesansbold.ttf”的基本字体。我已将下载的字体放在同一个文件夹中,但是当我使用
fontObj = pygame.font.Font('redline.ttf', 16)
textSurfaceObj = fontObj.render('some text', True, (240,240,240), (115,117,117))
textRectObj = textSurfaceObj.get_rect()
textRectObj.center = (350, 30)
DISPLAYSURF.blit(textSurfaceObj, textRectObj)
pygame.display.update()
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
OSError: unable to read font file 'redline.ttf'
Run Code Online (Sandbox Code Playgroud)
可以使用redline字体吗?
python ×12
path ×7
directory ×3
file ×3
filenames ×1
fonts ×1
pathlib ×1
pygame ×1
python-2.7 ×1
python-3.x ×1