os.path

How to handle OSX Aliases in Python with os.walk()?

帅比萌擦擦* 提交于 2019-12-10 20:35:24
问题 I'm traversing a directory tree using Python 2.7.x, getting the file and directory sizes as it traverses. The problem I'm running into is that it is mistaking alias files for directories, and then throwing an error that there's "No Such File or Directory". Code below: def get_size(start_path = '.'): total_size = 0 for dirpath, dirnames, filenames in os.walk(start_path): for f in filenames: fp = os.path.join(dirpath, f) total_size += os.path.getsize(fp) return total_size for dirName,

Pycharm “unresolved reference” on join of os.path

旧巷老猫 提交于 2019-12-07 02:02:18
问题 After upgrade pycharm to 2018.1, and upgrade python to 3.6.5, pycharm reports "unresolved reference 'join'". The last version of pycharm doesn't show any warning for the line below: from os.path import join, expanduser May I know why? (I used python 3.6.? before) I tried almost everything I can find, such as delete and recreate interpreter, invalidate cache and restart, delete and recreate virtualenv... how do I fix this? (I can run my program without any error.) 回答1: Sadly, it seems that

Pycharm “unresolved reference” on join of os.path

拜拜、爱过 提交于 2019-12-05 08:42:34
After upgrade pycharm to 2018.1, and upgrade python to 3.6.5, pycharm reports "unresolved reference 'join'". The last version of pycharm doesn't show any warning for the line below: from os.path import join, expanduser May I know why? (I used python 3.6.? before) I tried almost everything I can find, such as delete and recreate interpreter, invalidate cache and restart, delete and recreate virtualenv... how do I fix this? (I can run my program without any error.) Sadly, it seems that PyCharm will try to evaluate the path to an existing file/folder, which in some cases will not exist and thus

Rename files to instead being in sub-directories, have the year as part of the filename

走远了吗. 提交于 2019-12-02 17:28:44
问题 Create a copy of the CarItems tree called CarItemsCopy where all files, instead of being in directories named after years, rather have the year as part of the filename, and the year directories are entirely absent. Instead of some of these examples: CarItems/Chevrolet/Chevelle/2011/parts.txt CarItems/Chevrolet/Chevelle/1982/parts.txt CarItems/Chevrolet/Volt/1994/parts.txt it should look like this: CarItemsCopy/Chevrolet/Chevelle/parts-2011.txt CarItems/Chevrolet/Chevelle/parts-1982.txt

How to read an excel file directly from a Server with Python

喜夏-厌秋 提交于 2019-12-02 10:48:21
Scenario: I am trying to read a excel file from a server folder and after that read each worksheet of that file into a dataframe and perform some operations. Issue: I have trying multiple approaches but facing different situations: either I read the file, but it is seen as a str and the operations cannot be performed, or the file is not read. What I tried so far: #first attempt os.path(r'\\X\str\Db\C\Source\selection\Date\Test','r') #second attempt directory = os.getcwd() + "\\C\\Source\\selection\\Date\\Test" #third attempt f = os.getcwd() + "\\C\\Source\\selection\\Date\\Test\\12.xlsx"

Incrementing number in file name when file exists

杀马特。学长 韩版系。学妹 提交于 2019-12-02 07:18:27
I am still very new to Python (3). I have a BUNCH of sensor data, but the download limit forces me to retrieve the data in chunks instead of all at once (each .zip file downloaded contains a folder of .csv files for each sensor's data during a given time period). Thus, I have dozens of large .csv files distributed among several folders that I would eventually like to concat/merge/append into one .csv file for each sensor's full data. To make things more complicated, .csv file names for each sensor are identical across the folders. I have developed the following code to rename and move the

How/where to use os.path.sep?

好久不见. 提交于 2019-12-01 07:40:21
os.path.sep is the character used by the operating system to separate pathname components. But when os.path.sep is used in os.path.join() , why does it truncate the path? Example: Instead of 'home/python' , os.path.join returns '/python' : >>> import os >>> os.path.join('home', os.path.sep, 'python') '/python' I know that os.path.join() inserts the directory separator implicitly. Where is os.path.sep useful? Why does it truncate the path? Where os.path.sep is usefull? I suspect that it exists mainly because a variable like this is required in the module anyway (to avoid hardcoding), and if it

How/where to use os.path.sep?

六月ゝ 毕业季﹏ 提交于 2019-12-01 04:20:40
问题 os.path.sep is the character used by the operating system to separate pathname components. But when os.path.sep is used in os.path.join() , why does it truncate the path? Example: Instead of 'home/python' , os.path.join returns '/python' : >>> import os >>> os.path.join('home', os.path.sep, 'python') '/python' I know that os.path.join() inserts the directory separator implicitly. Where is os.path.sep useful? Why does it truncate the path? 回答1: Where os.path.sep is usefull? I suspect that it

Open File in Another Directory (Python)

情到浓时终转凉″ 提交于 2019-11-30 06:56:48
I've always been sort of confused on the subject of directory traversal in Python, and have a situation I'm curious about: I have a file that I want to access in a directory essentially parallel to the one I'm currently in. Given this directory structure: \parentDirectory \subfldr1 -testfile.txt \subfldr2 -fileOpener.py I'm trying to script in fileOpener.py to get out of subfldr2, get into subfldr1, and then call an open() on testfile.txt. From browsing stackoverflow, I've seen people use os and os.path to accomplish this, but I've only found examples regarding files in subdirectories beneath

Python os.path.join() on a list

半世苍凉 提交于 2019-11-30 05:56:11
问题 I can do >>> os.path.join("c:/","home","foo","bar","some.txt") 'c:/home\\foo\\bar\\some.txt' But, when I do >>> s = "c:/,home,foo,bar,some.txt".split(",") >>> os.path.join(s) ['c:/', 'home', 'foo', 'bar', 'some.txt'] What am I missing here? 回答1: The problem is, os.path.join doesn't take a list as argument, it has to be separate arguments. This is where * , the 'splat' operator comes into play... I can do >>> s = "c:/,home,foo,bar,some.txt".split(",") >>> os.path.join(*s) 'c:/home\\foo\\bar\