- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有以下代码:
os.chdir(os.path.dirname(os.path.realpath(__file__)) + "/../test")
path.append(os.getcwd())
os.chdir(os.path.dirname(os.path.realpath(__file__)))
应该将 /../test
添加到 python 路径,它确实这样做了,然后使用 PyDev 在 eclipse 上顺利运行。
但是当我从控制台第二个 os.chdir 午餐同一个应用程序时做错了,实际上错误的是 os.path.realpath(__file__)
cus 它返回 ../test/myFile.py
而不是 ../originalFolder/myFile.py
。当然,我可以通过使用固定的 os.chdir("../originalFolder")
来解决这个问题,但这对我来说似乎有点不对,但这适用于 eclipse 和控制台。
附言我正在使用 os.getcwd()
实际上是因为我想确保没有添加这样的文件夹,否则我根本不需要切换目录
那么我的方法有什么问题还是我搞砸了什么?还是什么? :)
提前致谢! :)
最佳答案
看看__file__
的值是什么。它不包含脚本的绝对路径,它是来自命令行的值,因此它可能类似于“./myFile.py”或“myFile.py”。此外,realpath() 不会使路径成为绝对路径,因此在不同目录中调用 realpath("myFile.py") 仍将返回“myFile.py”。
我认为你应该这样做:
import os.path
script_dir = os.path.dirname(os.path.abspath(__file__))
target_dir = os.path.join(script_dir, '..', 'test')
print(os.getcwd())
os.chdir(target_dir)
print(os.getcwd())
os.chdir(script_dir)
print(os.getcwd())
在我的电脑 (Windows) 上我有这样的结果:
e:\parser>c:\Python27\python.exe .\rp.py
e:\parser
e:\test
e:\parser
e:\parser>c:\Python27\python.exe ..\parser\rp.py
e:\parser
e:\test
e:\parser
注意:如果您关心兼容性(您不喜欢奇怪的路径错误),您应该在组合路径时使用os.path.join()。
注意:我知道我的解决方案非常简单(记住绝对路径),但有时最简单的解决方案是最好的。
关于python os.path.realpath 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9887259/
出于性能原因,我们应该只使用 realpath()而不是 realpath() + file_exists()在检查文件或目录是否存在时 ?? 案例 A:: if(realpath(__DIR__."
做这样的事情是不是一种感觉 $dir = realpath(dirname(__FILE__)); // or for php 5.3+: $dir = realpath(__DIR__) 或者
调用 Unix realpath() 函数的传统方法是 realpath(pathname, buf) 其中 buf 是用户提供的PATH_MAX 字节空间的缓冲区。这是有问题的,因为 PATH_MA
这个问题在这里已经有了答案: Unexpected behavior in printing a char array in C (1 个回答) 关闭 8 年前。 这是我的功能,它在给定目录中查找常
PHP 的 realpath() 在 manual 中有详细描述,我只是想了解它在哪些场景下有用。因为看起来我的代码可以很好地处理 ../../path 这样的路径,所以我不确定 realpath()
我使用 MediaWiki,但出现错误。我在文件中找到了它。有一行代码: $IP = realpath( '.' ); 但是 $IP 返回一个空字符串,所以系统中文件的路径不起作用。 (例如,如果 $
我想检查一个路径是否是另一个路径的子路径,所以我调用 realpath(file_path, NULL) 并将结果与 strncmp 进行比较。 我写这个主题的原因是:realpath 设置err
我在使用 realpath 函数时遇到了一个奇怪的问题。该函数在给定一个作为程序参数接收的字符串时工作,但在给定我在源代码中定义的字符串时失败。这是一个简单的程序: #include #includ
我正在开发一个程序,它使用 realpath() 来获取文件的绝对路径。不幸的是,这个函数需要一个字符串缓冲区,这个缓冲区应该很大,以至于它足够大,当这个应用程序必须跨多个平台运行时,这是不安全的。是
给定 现代 Linux/UNIX/OSX(带 realpath) bash 4+(甚至在 OSX 上) 是 "$PWD" == "$(realpath .)" 总是正确的? 最佳答案 很容易测试情况并
我的 Windows 服务器上有一个符号链接(symbolic link),它是这样制作的: F:\>mkdir link-target F:\>mklink /D link f:\link-targ
当我编译以下代码时: #define _POSIX_C_SOURCE 200112L #define _ISOC99_SOURCE #define __EXTENSIONS__ #include #
我在使用 File.realpath() 时遇到问题,留下的字符串似乎没有被垃圾收集。在我看来,这像是内存泄漏,但我无法想象这样的事情对于核心库方法来说真的是真的。 考虑以下代码: def strin
我已经养成了使用以下命令在我的编辑器中快速打开相关文件进行代码审查的习惯。 git diff --name-only master... | xargs realpath | someEditor 但
我的一个扩展要求我将 realpath_cache_size 设置为至少 512k。那么我是否必须将它设置在 .htaccess 中,例如: realpath_cache_size = 512k 以及
我已经读过有关 realpath() 的内容,但是是否有一个函数可以传递基本目录和文件名,可以在不解析符号链接(symbolic link)或检查文件是否实际存在的情况下给出以下结果?或者我是否必须使
我已经阅读过有关 realpath() 的内容,但是是否有一个函数可以传递一个基本目录和一个文件名,从而在不解析符号链接(symbolic link)或检查文件是否实际存在的情况下给出以下结果?还是我
我想获取当前执行脚本的路径,我用过 os.path.realpath(__file__),但是,它会返回类似 D:\My Stuff\Python\my_script.py 的字符串,而没有适当的反斜
显然,realpath 有很多问题。在 PHP 5.3.1 中,它会导致随机崩溃。在 5.3.0 及更低版本中,realpath 随机失败并返回 false(当然是针对相同的字符串),而且它总是在 r
我有以下代码: os.chdir(os.path.dirname(os.path.realpath(__file__)) + "/../test") path.append(os.getcwd())
我是一名优秀的程序员,十分优秀!