首页 > 编程语言 >[888] How to get the directory of the current Python file

[888] How to get the directory of the current Python file

时间:2023-09-26 09:12:25浏览次数:27  
标签:__ get Python 888 current file directory path

To get the directory of the current Python file, you can use the os.path module in combination with the __file__ attribute. Here's how you can do it:

import os

# Get the directory of the current Python file
current_directory = os.path.dirname(os.path.abspath(__file__))

print("Directory of the current file:", current_directory)

In this code:

  • __file__ is a built-in attribute in Python that represents the path to the current Python script.
  • os.path.abspath(__file__) obtains the absolute path to the current Python script.
  • os.path.dirname() extracts the directory part from the absolute path.

After running this code, current_directory will contain the directory of the current Python file. You can then use this directory for various purposes, such as reading or writing files in the same directory.

标签:__,get,Python,888,current,file,directory,path
From: https://www.cnblogs.com/alex-bn-lee/p/17729333.html

相关文章

  • # yyds干货盘点 # 盘点一个使用Python自动化处理GPS、北斗经纬度数据实战(下篇)
    大家好,我是皮皮。一、前言上一篇文章我们使用了Python来实现数据的导入和分列处理,最终可以得到符合预期的结果,不过还可以继续深挖优化下,这一篇文章一起来看看吧。优化的背景如下图所示:二、实现过程这里【瑜亮老师】继续给了一个优化指导,如下图所示:并且给出的代码如下:withopen("./G......
  • 盘点一个使用Python自动化处理GPS、北斗经纬度数据实战(下篇)
    大家好,我是皮皮。一、前言上一篇文章我们使用了Python来实现数据的导入和分列处理,最终可以得到符合预期的结果,不过还可以继续深挖优化下,这一篇文章一起来看看吧。优化的背景如下图所示:二、实现过程这里【瑜亮老师】继续给了一个优化指导,如下图所示:并且给出的代码如下:with......
  • Python RuntimeError: dictionary changed size during iteration
    运行下面代码,报如下错误fornameinglobals():print(name) 解决办法是:将待遍历的对象转换成列表fornameinlist(globals()):print(name) ......
  • poython文件运行模式和python控制台运行模式和Juputer运行模式
    今天学pytorch,我知道了python文件是整个代码运行,,而python控制台是一行一行的运行。而Juputer是任意代码块运行的。 ......
  • Python与Java的语法区别
    数据容器/数组/集合Python:对数据容器的操作#对list进行切片,从1开始,4结束,步长1(默认步长为1)my_list=[0,1,2,3,4,5,6]result1=my_list[1:4]print(f"结果1:{result1}")#对tuple进行切片,从头开始,到最后结束,步长1my_tuple=(0,1,2,3,4,5,6)result2=my_tu......
  • Python-day18
    1、常用的文件打开模式rfile=open('a.txt','r')print(file.readlines())file.close()wfile=open('a.txt','w')file.write('whywhywhy')file.close()afile=open('a.txt','a')file.write('whywhywhy......
  • Python学习笔记1
    a="好的,测试字符tester"b=17c=3print(a[1:5])#从第1(包含)个字符取到第5(不包含)个字符print(a[:3])#取到第3个字符(不含3)print(a[-5:-1])#取倒数第5个到倒数第1个print(a[-1:])#取最后一个字符print(len(a))#字符长度#exit()#退出与quit()一样,里面......
  • python2 http服务端和客户端
    server.pyimportSimpleHTTPServerimportSocketServerclassMyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):defdo_GET(self):self.send_response(200)self.send_header('Content-type','text/plain')self......
  • Python的Selenium库:网页元素定位工具
    Selenium是一个用于自动化web应用测试的开源工具。通过Selenium,我们可以模拟真实用户的操作,如点击、输入、滚动页面等,来测试web应用的稳定性和可靠性。PythonSelenium库是Selenium的一个分支,可以方便地与Python语言结合使用。在PythonSelenium库中,元素定位是一项核心功能。通过......
  • urllib_ajax的get请求豆瓣电影第一页
    #get请求#获取豆瓣电影的第一页的数据并且保存起来importurllib.requesturl='https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&start=0&limit=20'headers={'User-Agent':'Mozilla/5.0(WindowsNT......