简体   繁体   English

这个获取当前文件路径的 Python 代码将在 Linux 上工作吗?

[英]This Python code to get the current file path will work on Linux?

Sorry if it's a stupid question, I don't know anything about Linux and I've had problems with paths in Linux, will this Python code I made in Windows work in Linux? Sorry if it's a stupid question, I don't know anything about Linux and I've had problems with paths in Linux, will this Python code I made in Windows work in Linux?

Code:代码:

File_Path = str(Path(__file__).parent.absolute())
User_Dir = File_Path + '/data'

If you can explain more about the compatibility of Python codes from Windows to Linux I would appreciate it.如果您能详细解释 Python 代码从 Windows 到 Linux 的兼容性,我将不胜感激。

For me, it will work, but I have 2 questions:对我来说,它会起作用,但我有两个问题:

  • Why don't you use Path Library more?为什么不更多地使用路径库? Like this:像这样:
 File_Path = Path(__file__).parent.absolute() User_Dir = File_Path.join('data')
  • and user_dir we are agree it's the directory "data" next to your sourcefile?和 user_dir 我们同意它是源文件旁边的目录“数据”?

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

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