Python中获取文件路径

Python 如何获取文件路径? 【代码】Python 如何获取文件路径? 阅读详情

os.path.abspath()

os.path.abspath() : 获取文件或目录的绝对路径
     print(os.path.abspath('./'))
     	C:\Users\###\Desktop\myproject\data
     print(os.path.abspath('aa.ini'))
     	C:\Users\###\Desktop\myproject\data\aa.ini

os.path.dirname()

os.path.dirname(__file__) : 获取当前文件的路径
     print(os.path.abspath('./'))
     	C:\Users\###\Desktop\myproject\data
os.path.dirname(os.path.dirname(__file__)) :获取当前文件上一层目录中文件的路径
     print(os.path.abspath('./'))
     	C:\Users\###\Desktop\myproject

os.path.normpath() 更据平台不同调整路径, 使路径符合当前平台

如:os.path.normpath('/aa/bb/cc/d.txt’)
	linux中输出结果为:'/aa/bb/cc/d.txt’
	windows中输出结果为:'\aa\bb\cc\d.txt’

os.path.join(路径, 文件名):拼接文件路径

print(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'aa.ini'))
	C:\Users\###\Desktop\myproject\data\aa.ini

os.path.relpath(原路径, 要从原路径中切掉的路径): 切割路径

src_path = 'C:\\Users\\61980\\Desktop\\pyzo\\pyzo\\pyzo\\resources\\translations\\pyzo_sk_SK.tr'
cut_path = 'C:\\Users\\61980\\Desktop\\pyzo\\pyzo'
rst_path = os.path.relpath(src_path, cut_path)
rst_path  :  'pyzo\\resources\\translations\\pyzo_sk_SK.tr'

os.path.split(path):获取文件名和文件所在目录路径

path = 'C:\\Users\\61980\\Desktop\\pyzo\\pyzo\\pyzo\\resources\\translations\\pyzo_sk_SK.tr'
file_path, file_name = os.path.split(path)
file_path :'C:\\Users\\61980\\Desktop\\pyzo\\pyzo\\pyzo\\resources\\translations‘
fiel_name:pyzo_sk_SK.tr

解决路径问题 : 获取myproject目录中cc.txt的路径

cur = os.path.dirname(os.path.abspath(__file__))  :获取文件所在当前目录的绝对路径 ,等于os.getcwd()
per = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  :获取当前文件上一层目录的绝对路径
print(os.path.join(per , 'cc.txt'))
	C:\Users\###\Desktop\myproject\cc.txt

os获取当前用户主目录路径:os.path.expanduser(’~’)

附录B:Standard Delay Format(SDF)(上) 本附录将介绍标准延迟标注格式,并说明了如何在仿真中执行反标。 延迟格式描述了设计网表的单元延迟和互连走线延迟,无论设计是用两种主要硬件描述语言(VHDL或Verilog HDL)中的哪一种所描述的。 本章还会介绍仿真的反标(backannotation),STA的反标其实是一个简单直接的过程,其中DUA中的时序弧将由SDF所指定的延迟进行标注。 阅读详情

相关推荐

【VAE学习笔记】全面通透地理解VAE(Variational Auto Encoder)

完整笔记:http://www.gwylab.com/note-vae.html 李宏毅老师的教程视频:https://www.bilibili.com/video/av15889450/?p=33 —————————————————————————————————   依据李宏毅老师的讲解,我整理了一番VAE的笔记。先简单介绍一下VAE,VAE作为一个生成模型,其基本思路是很容易理解的:把一堆真...

a312863063的博客 14万+

python查看指定文件的绝对路径和当前工作路径

查看某一指定文件的绝对路径使用os.path.abspath('指定文件名'),举例如下: 在python窗口输入os.path.abspath('edge_detect.py'),则输入了我指定的edge_detect.py这个文件的绝对路径。

re_call的博客 4311

适配adv7280m相机到rk3588的安卓平台

Kernel-5.10/arch/arm64/configs/firefly_defconfig里面增加配置CONFIG_VIDEO_ADV7180=y。出现一次开不了机器的情况,接调试口发现,在set_fmt里面用s_ctrl对mipi的频率进行设置会出现异常。设备树修改kernel-5.10/arch/arm64/boot/dts/rockchip/13.dts。把MEDIA_ENT_F_ATV_DECODER改回MEDIA_ENT_F_CAM_SENSOR。第二次编译,仅仅刷新boot.img。

chenyq14的专栏 2449

Python文件路径操作汇总,获取文件夹下的所有文件路径

在Python编程过程中,我们常常有这样的需求,需要获取某一个文件目录下的所有文件,或获取文件目录下的所有指定后缀名的文件,亦或要求获取该文件所在的文件夹,还有可能需要替换文件名,删除文件夹。针对这些问题,本文系统总结了这些方法,以供大家参考。

程序员小麦的博客 8317

从零开始,手把手教你用RTL代码设计一个简单的8位CPU(附状态图与ALU设计)

本文详细介绍了如何从零开始设计一个简单的8位CPU,包括状态机设计、ALU实现与RTL编码实战。通过具体的指令集分析、状态图设计和Verilog代码示例,帮助读者深入理解CPU工作原理,并掌握RTL代码编写技巧。特别适合计算机系统学习者和硬件设计爱好者。

weixin_30847063的博客 320

f103vet6 SD卡SDIO+DMA配置避坑指南:从初始化卡死到稳定读写的全流程解析

本文详细解析STM32F103VET6芯片SD卡SDIO与DMA配置中的常见问题,从初始化卡死到读写不稳定的全流程解决方案。重点分析总线宽度配置、时钟分频调整及DMA通道管理技巧,帮助开发者避开常见陷阱,实现稳定高效的数据存储。

u0v1w2x3的博客 666

Python获取与处理文件路径/目录路径

Python程序基于os库完成获取与处理文件路径和目录路径的方法

qq_45899597的博客 4万+

【gridsample】地平线征程6芯片中gridsample算子的优化与部署实践

本文详细介绍了地平线征程6芯片中gridsample算子的优化与部署实践,重点探讨了PTQ和QAT量化方案在BEV感知任务中的应用。通过双线性插值优化、内存布局调整和混合精度配置等关键技术,显著提升了算子在自动驾驶场景下的性能和精度,为开发者提供了实用的部署指南和调优建议。

zero1的博客 1019

python 获取当前文件路径

若按相对路径来直接执行脚本./pyws/path_demo.py,会得到相对路径。若显示调用python指令,如python demo.py,会得到绝对路径;sys.argv[0]|获得模块所在的路径(由系统决定是否是全名)将文件路径名称分成头和尾一对,生成二元元组。(文件目录,文件名)若直接执行脚本,如./demo.py,会得到相对路径。获得文件所在的路径(由系统决定是否是全名)若显示执行Python,会得到绝对路径;获取文件当前工作目录路径(绝对路径)获得文件所在的路径(绝对路径)

cuisidong1997的博客 1万+

基于STM32单片机设计的智能水温控制系统

本项目研究并实现了一种基于STM32单片机的智能水温控制系统。系统以STM32F103RCT6作为主控制器,通过DS18B20温度传感器实时采集水温数据,利用PID控制算法精确调节5V直流防水型70W加热棒的功率输出,确保水温恒定在设定范围内。此外,采用OLED显示屏显示当前水温和预设上限阀值,并集成ESP8266-WiFi模块与自研Android手机APP进行远程通信和控制。用户可通过本地按键或手机APP设置温度阀值,实现实时监控与智能化管理。整个系统设计旨在提高水资源利用效率,提升生活及工业用水环境的舒

8790

量化交易 米筐 多因子合成(回测中使用)

多因子合成 1、因子合成 将一些相关(高、低)的因子合成一个因子 方法: 使用PCA方式(使用于较强相关性) 2、核心代码 将因子 inc_revenue_ttm 和 operating_revenue_growth_ratio_ttm 合成为一个因子 inc_operating from sklearn.decomposition import PCA pca = PCA(n_components=1) # 因子合并 fund['inc_operating'] = pca.fit_transform(fu

廷益_飞鸟的博客 1062

python获取文件路径

import os root_path = os.path.abspath(os.path.join(os.getcwd(), "..")) print("""*********************** Path test:start..... ********************""") print("***获得当前路径***") cwd = os.getcwd() print(...

Poo_Chai的博客 1万+

Python爬虫实战:逆向工程与异步爬虫技术,高效抓取国家知识产权局专利数据

相比于传统的Selenium,Playwright由微软开发,支持多浏览器(Chromium, Firefox, WebKit),API更现代,执行速度更快,并且能轻松录制用户操作生成代码。通常,列表中的摘要信息是缩略的,我们需要进入每个专利的详情页获取完整信息(如主权要求、法律状态等)。我们的第一步是手动操作一遍搜索流程,并用Playwright的“录制”功能或开发者工具,找出真正返回专利数据的那一个或多个网络请求。以友好的、对人类行为模拟的方式爬取数据是基本的网络礼仪,也能有效避免你的IP被封锁。

2201_76125261的博客 2419

python 如何获取文件路径_Python如何获取文件路径/目录

一、获取文件路径实现1.1 获取当前文件路径import oscurrent_file_path = __file__print(f"current_file_path: {current_file_path}")__file__变量其实有个问题,当文件被是被调用文件时__file__总是文件的绝对路径;但当文件是直接被执行的文件时,__file__并不总是文件的绝对路径,而是你执行该文件时给py...

weixin_39779004的博客 1161

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

python获取某文件路径_python获取当前文件路径以及父文件路径的方法

#当前文件的路径pwd = os.getcwd()#当前文件的父路径father_path=os.path.abspath(os.path.dirname(pwd)+os.path.sep+".")#当前文件的前两级目录grader_father=os.path.abspath(os.path.dirname(pwd)+os.path.sep+"..")追加部分代码实例def TestPrtPwd...

weixin_39665060的博客 1167

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

python 当前文件路径获取方式_python获取当前文件路径以及父文件路径的方法

#当前文件的路径pwd = os.getcwd()#当前文件的父路径father_path=os.path.abspath(os.path.dirname(pwd)+os.path.sep+".")#当前文件的前两级目录grader_father=os.path.abspath(os.path.dirname(pwd)+os.path.sep+"..")追加部分代码实例def TestPrtPwd...

weixin_39912250的博客 349

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

python获取某文件路径_Python获取当前文件路径

一. Python 获取当前文件路径方法2. sys.path[0]获取文件当前工作目录路径(绝对路径)sys.argv[0]|获得模块所在的路径(由系统决定是否是全名)若显示调用python指令,如python demo.py,会得到绝对路径;若直接执行脚本,如./demo.py,会得到相对路径。3. __ file __获得文件所在的路径(由系统决定是否是全名)若显示执行Python,会得到绝...

weixin_39549110的博客 7225

python获取某文件路径_python怎么获取当前文件路径

Python获取当前路径的方法:import os,sys使用sys.path[0]、sys.argv[0]、os.getcwd()、os.path.abspath(__file__)、os.path.realpath(__file__)sys.path是Python会去寻找模块的搜索路径列表,sys.path[0]和sys.argv[0]是一回事因为Python会自动把sys.argv[0]加入...

weixin_40003233的博客 2829

Python获取当前文件路径

在Python中,你可以使用os模块或pathlib模块来获取当前执行文件的路径。

Leon_Jinhai_Sun的博客 638

python 当前文件路径获取方式_Python获取当前文件路径

一. Python 获取当前文件路径方法2. sys.path[0]获取文件当前工作目录路径(绝对路径)sys.argv[0]|获得模块所在的路径(由系统决定是否是全名)若显示调用python指令,如python demo.py,会得到绝对路径;若直接执行脚本,如./demo.py,会得到相对路径。3. __ file __获得文件所在的路径(由系统决定是否是全名)若显示执行Python,会得到...

weixin_33149211的博客 9676

python如何获取文件路径_Python如何获取文件路径/目录

一、获取文件路径实现1.1 获取当前文件路径import oscurrent_file_path = __file__print(f"current_file_path: {current_file_path}")__file__变量其实有个问题,当文件被是被调用文件时__file__总是文件的绝对路径;但当文件是直接被执行的文件时,__file__并不总是文件的绝对路径,而是你执行该文件时给py...

weixin_39668898的博客 1453

python如何获取文件的路径_Python如何获取文件路径/目录

一、获取文件路径实现1.1 获取当前文件路径import oscurrent_file_path = __file__print(f"current_file_path: {current_file_path}")__file__变量其实有个问题,当文件被是被调用文件时__file__总是文件的绝对路径;但当文件是直接被执行的文件时,__file__并不总是文件的绝对路径,而是你执行该文件时给py...

weixin_39818691的博客 1615

python——获取当前文件路径

1、os.getcwd(): os.getcwd()不需要参数 os.getcwd()获取的是当前工作的路径,也就是脚本运行的地方,而不是脚本本身所存在的地方 代码: print(os.getcwd()) 测试结果: 2、os.path.abspath(): os.path.abspath()需要提供一个参数 若指定的参数是 "."(获取当前目录),r".."(获取当前目录的上级目录),则获取的是工作路径 若给定的参数是文件路径,则返回文件路径,包含文件名 若要获得所在文件的文件.

liu_feng_zi_的专栏 2万+

python获取某文件路径_python获取当前文件路径

python获取当前文件路径importos,sys使用sys.path[0]、sys.argv[0]、os.getcwd()、os.path.abspath(__file__)、os.path.realpath(__file__)sys.path是Python会去寻找模块的搜索路径列表,sys.path[0]和sys.argv[0]是一回事因为Python会自动把sys.argv[0]加入sy...

weixin_39700548的博客 1264
下一篇: 使用sphinx生成python项目的api文档(html格式的api文档)
YPFeime
博客等级 码龄9年 19粉丝 49原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值