os.path.abspath(__ file__)给出无效的位置,并在文件路径中添加额外的\

时间:2019-09-18 17:43:07

标签: python windows path

我正在开发一个程序,该程序将编辑所有以csv扩展名结尾的本地文件。当我调用目录的位置然后更改目录时,出现错误。该错误是由于在路径中添加了多余的\。没有这些多余的\怎么称呼路径?

我环顾四周,也遇到了类似的问题,但是我看到的每个示例都是针对一个硬文字位置,而不是一个可移动位置。

import os
import glob
import sys


path = os.path.abspath(__file__)
extension = '.csv'
os.chdir(os.path.abspath(__file__))
result = glob.glob('*'.format(extension))
print(path)
print(result)

1 个答案:

答案 0 :(得分:2)

cd "/Users/zhouyuhongze/Downloads/cove_dev copy-64-9.3" export LANG=en_US.US-ASCII /Applications/Xcode9.3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++-header -arch ppc -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -gmodules -Wno-trigraphs -fpascal-strings -O0 -Wno-missing-field-initializers -Wno-missing-prototypes -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wno-empty-body -Wno-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wno-constant-conversion -Wno-int-conversion -Wno-bool-conversion -Wno-enum-conversion -Wno-float-conversion -Wno-non-literal-null-conversion -Wno-objc-literal-conversion -Wno-shorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -DMAC -DUWWF_EXPORTS -isysroot /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof -mmacosx-version-min=10.14 -g -fvisibility-inlines-hidden -Wno-sign-conversion -Wno-infinite-recursion -Wno-move -Wno-comma -Wno-block-capture-autoreleasing -Wno-strict-prototypes -Wno-range-loop-analysis -index-store-path /Users/zhouyuhongze/Library/Developer/Xcode/DerivedData/cove-brktkbotsuuztjcusjhbbaohvtrb/Index/DataStore -I/Users/zhouyuhongze/Library/Developer/Xcode/DerivedData/cove-brktkbotsuuztjcusjhbbaohvtrb/Build/Intermediates.noindex/cove.build/Debug/uwwf_python.build/_uwwf_python.hmap -I/Users/zhouyuhongze/Library/Developer/Xcode/DerivedData/cove-brktkbotsuuztjcusjhbbaohvtrb/Build/Products/Debug/include -Ibase -Ibase/vl/include -Iidv -Ilib/fltk -Ilib/netcdf -I/Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -I/Users/zhouyuhongze/Library/Developer/Xcode/DerivedData/cove-brktkbotsuuztjcusjhbbaohvtrb/Build/Intermediates.noindex/cove.build/Debug/uwwf_python.build/DerivedSources/ppc -I/Users/zhouyuhongze/Library/Developer/Xcode/DerivedData/cove-brktkbotsuuztjcusjhbbaohvtrb/Build/Intermediates.noindex/cove.build/Debug/uwwf_python.build/DerivedSources -F/Users/zhouyuhongze/Library/Developer/Xcode/DerivedData/cove-brktkbotsuuztjcusjhbbaohvtrb/Build/Products/Debug -MD -MT dependencies -MF /Users/zhouyuhongze/Library/Developer/Xcode/DerivedData/cove-brktkbotsuuztjcusjhbbaohvtrb/Build/Intermediates.noindex/PrecompiledHeaders/cove_Prefix-gvxvqsoycewanvflcbunhiimhfnz/cove_Prefix.pch.d -c /Users/zhouyuhongze/Downloads/cove_dev\ copy-64-9.3/cove_Prefix.pch -o /Users/zhouyuhongze/Library/Developer/Xcode/DerivedData/cove-brktkbotsuuztjcusjhbbaohvtrb/Build/Intermediates.noindex/PrecompiledHeaders/cove_Prefix-gvxvqsoycewanvflcbunhiimhfnz/cove_Prefix.pch.pch --serialize-diagnostics /Users/zhouyuhongze/Library/Developer/Xcode/DerivedData/cove-brktkbotsuuztjcusjhbbaohvtrb/Build/Intermediates.noindex/PrecompiledHeaders/cove_Prefix-gvxvqsoycewanvflcbunhiimhfnz/cove_Prefix.pch.dia In file included from /Users/zhouyuhongze/Downloads/cove_dev copy-64-9.3/cove_Prefix.pch:5: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/Carbon.framework/Headers/Carbon.h:20: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreServices.framework/Headers/CoreServices.h:19: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CoreFoundation.h:16: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/types.h:75: /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/cdefs.h:763:2: error: Unsupported architecture #error Unsupported architecture ^ In file included from /Users/zhouyuhongze/Downloads/cove_dev copy-64-9.3/cove_Prefix.pch:5: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/Carbon.framework/Headers/Carbon.h:20: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreServices.framework/Headers/CoreServices.h:19: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CoreFoundation.h:16: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/types.h:78: /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/machine/types.h:37:2: error: architecture not supported #error architecture not supported ^ In file included from /Users/zhouyuhongze/Downloads/cove_dev copy-64-9.3/cove_Prefix.pch:5: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/Carbon.framework/Headers/Carbon.h:20: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreServices.framework/Headers/CoreServices.h:19: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CoreFoundation.h:16: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/types.h:79: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/_types.h:33: /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/machine/_types.h:34:2: error: architecture not supported #error architecture not supported ^ In file included from /Users/zhouyuhongze/Downloads/cove_dev copy-64-9.3/cove_Prefix.pch:5: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/Carbon.framework/Headers/Carbon.h:20: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreServices.framework/Headers/CoreServices.h:19: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CoreFoundation.h:16: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/types.h:79: /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/_types.h:55:9: error: unknown type name '__int64_t' typedef __int64_t __darwin_blkcnt_t; /* total blocks */ ^ /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/_types.h:56:9: error: unknown type name '__int32_t' typedef __int32_t __darwin_blksize_t; /* preferred block size */ ^ /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/_types.h:57:9: error: unknown type name '__int32_t' typedef __int32_t __darwin_dev_t; /* dev_t */ ^ /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/_types.h:60:9: error: unknown type name '__uint32_t' typedef __uint32_t __darwin_gid_t; /* [???] process and group IDs */ ^ /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/_types.h:61:9: error: unknown type name '__uint32_t' typedef __uint32_t __darwin_id_t; /* [XSI] pid_t, uid_t, or gid_t*/ ^ /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/_types.h:62:9: error: unknown type name '__uint64_t' typedef __uint64_t __darwin_ino64_t; /* [???] Used for 64 bit inodes */ ^ /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/_types.h:68:9: error: unknown type name '__darwin_natural_t' typedef __darwin_natural_t __darwin_mach_port_name_t; /* Used by mach */ ^ /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/_types.h:70:9: error: unknown type name '__uint16_t' typedef __uint16_t __darwin_mode_t; /* [???] Some file attributes */ ^ /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/_types.h:71:9: error: unknown type name '__int64_t' typedef __int64_t __darwin_off_t; /* [???] Used for file sizes */ ^ /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/_types.h:72:9: error: unknown type name '__int32_t' typedef __int32_t __darwin_pid_t; /* [???] process and group IDs */ ^ /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/_types.h:73:9: error: unknown type name '__uint32_t' typedef __uint32_t __darwin_sigset_t; /* [???] signal set */ ^ /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/_types.h:74:9: error: unknown type name '__int32_t' typedef __int32_t __darwin_suseconds_t; /* [???] microseconds */ ^ /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/_types.h:75:9: error: unknown type name '__uint32_t' typedef __uint32_t __darwin_uid_t; /* [???] user IDs */ ^ /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/_types.h:76:9: error: unknown type name '__uint32_t' typedef __uint32_t __darwin_useconds_t; /* [???] microseconds */ ^ In file included from /Users/zhouyuhongze/Downloads/cove_dev copy-64-9.3/cove_Prefix.pch:5: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/Carbon.framework/Headers/Carbon.h:20: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreServices.framework/Headers/CoreServices.h:19: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CoreFoundation.h:16: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/types.h:81: /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/machine/endian.h:37:2: error: architecture not supported #error architecture not supported ^ In file included from /Users/zhouyuhongze/Downloads/cove_dev copy-64-9.3/cove_Prefix.pch:5: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/Carbon.framework/Headers/Carbon.h:20: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreServices.framework/Headers/CoreServices.h:19: In file included from /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CoreFoundation.h:16: /Applications/Xcode9.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/types.h:95:9: error: unknown type name 'u_int64_t' typedef u_int64_t u_quad_t; /* quads */ ^ fatal error: too many errors emitted, stopping now [-ferror-limit=] 20 errors generated.``` [1]: https://i.stack.imgur.com/LmnLG.png 需要一个目录而不是您要提供的文件。尝试将os.chdir()更改为os.chdir(os.path.abspath(__file__))

os.chdir(os.path.dirname(path))