os.path

file exists or not through matching filename in list

空扰寡人 提交于 2020-05-17 07:34:05
问题 i have files in folders and subfolders. folder structure is like this 2020(folder) -01(sub folder) --14(sub-sub folder) ----abc1-2020-01-14.csv ----abc2-2020-01-14.csv -02(subfolder in 2020) --17(sub-sub folder in 02) ----abc1-2020-02-17.csv ----abc4-2020-02-17.csv i have list of file names. li = ['abc1','abc2','abc3','abc4'] i want to know if these file exists in directory or not. each subdirectory should have all 4 files. if not then code must return path where particular file doesnot exist

Python os.path.relpath behavior

匆匆过客 提交于 2019-12-31 22:24:49
问题 I have a directory bar inside a directory foo , with file foo_file.txt in directory foo and file bar_file.txt in directory bar ; i.e. computer$ ls foo/ computer$ ls foo/ bar/ foo_file.txt computer$ ls foo/bar/ bar_file.txt Using the python os.path.relpath function, I expect: os.path.relpath('foo/bar/bar_file.txt', 'foo/foo_file.txt') to give me: 'bar/bar_file.txt' However, it actually gives me: '../bar/bar_file.txt' Why is this? Is there an easy way to get the behavior I want? EDIT: This is

Python os.path.relpath behavior

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-31 22:24:22
问题 I have a directory bar inside a directory foo , with file foo_file.txt in directory foo and file bar_file.txt in directory bar ; i.e. computer$ ls foo/ computer$ ls foo/ bar/ foo_file.txt computer$ ls foo/bar/ bar_file.txt Using the python os.path.relpath function, I expect: os.path.relpath('foo/bar/bar_file.txt', 'foo/foo_file.txt') to give me: 'bar/bar_file.txt' However, it actually gives me: '../bar/bar_file.txt' Why is this? Is there an easy way to get the behavior I want? EDIT: This is

Searching for a USB in Python is returning 'there is no disk in drive'

心不动则不痛 提交于 2019-12-25 14:04:33
问题 I wrote up a function in Python that looks for a USB drive based on a key identifier file, however when called upon it returns 'There is no disk in the drive. Please insert a disk into drive D:/' (which is an SD card reader) - is there a way to have it search drive letters based on drives that are 'ready'? def FETCH_USBPATH(): for USBPATH in ascii_uppercase: if os.path.exists('%s:\\File.ID' % USBPATH): USBPATH='%s:\\' % USBPATH print('USB is mounted to:', USBPATH) return USBPATH + "" return "

Python finds a string in multiple files recursively and returns the file path

两盒软妹~` 提交于 2019-12-25 07:27:37
问题 I'm learning Python and would like to search for a keyword in multiple files recursively. I have an example function which should find the *.doc extension in a directory. Then, the function should open each file with that file extension and read it. If a keyword is found while reading the file, the function should identify the file path and print it. Else, if the keyword is not found, python should continue. To do that, I have defined a function which takes two arguments: def find_word

Python - rename files in subfolders based on subfolder and file name

喜你入骨 提交于 2019-12-23 06:11:38
问题 I have a folder, C:\temp, with subfolders and files like below: \11182014\ VA1122F.A14 VA9999N.A14 CT3452F.B13 CT1467A.B14 \12012014\ MT4312F.B14 MT4111N.B14 CT4111F.A12 The file extensions are always an ".A" or ".B" followed by 2 digits. The file names always end with an "F", "A", or "N". I would like to loop through all subfolders in C:\temp and: prefix each file with "My_X_" where X is either an F, N, or A (i.e., the last letter in the file name) suffix each file with "_" + the name of the

Incrementing number in file name when file exists

牧云@^-^@ 提交于 2019-12-20 04:38:37
问题 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

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

你离开我真会死。 提交于 2019-12-18 09:48:15
问题 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\

Open File in Another Directory (Python)

ε祈祈猫儿з 提交于 2019-12-17 22:51:37
问题 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

double backslash python os.path.abspath

与世无争的帅哥 提交于 2019-12-11 23:19:48
问题 I get the path dire=os.path.abspath(".") and for fileName in filter(os.path.isfile, os.listdir(path=direc)) but dire has C:\\ and sends me the next error: TypeError: listdir() takes no keyword arguments when I print dire to see the content print next: C:\\user\\documents.... what can I do to get \ and not \\ in os.path.abspath(".") ? 回答1: I'm assuming that by print you mean repr . s = 'C:\\' s >>> 'C:\\' print(s) >> C:\ Note that while printing there aren't neither double \\ nor ' The other