python-os

Use multiple file extensions for glob to find files

社会主义新天地 提交于 2019-12-23 13:15:23
问题 I am using glob to list out all my python files in the home directory by this line of code. I want to find all .json files too along with py files, but I couldn't found any to scan for multiple file types in one line code. for file in glob.glob('/home/mohan/**/*.py', recursive=True): print(file) 回答1: You could use os.walk, which looks in subdirectories as well. import os for root, dirs, files in os.walk("path/to/directory"): for file in files: if file.endswith((".py", ".json")): # The arg can

How to quickly get the last line from a .csv file over a network drive?

旧街凉风 提交于 2019-12-23 10:24:11
问题 I store thousands of time series in .csv files on a network drive. Before I update the files, I first get the last line of the file to see the timestamp and then I update with data after that timestamp. How can I quickly get the last line of a .csv file over a network drive so that I don't have to load the entire huge .csv file only to use the last line? 回答1: There is a nifty reversed tool for this, assuming you are using the built-in csv module: how to read a csv file in reverse order in

How to create directories and sub directories efficiently and elegantly in Python 2.7?

此生再无相见时 提交于 2019-12-22 10:20:00
问题 I am trying to create a bunch of directories and sub directories at a specific location in my PC. My process is something like this: Check if there's any directory with the same directory name. Skip if so. If not, create the directory and the pre-defined sub directories under that directory. This is the code I came up with using os module: def Test(): main_dir = ["FolderA", "FolderB"] common_dir = ["SubFolder1", "SubFolder2", "SubFolder3"] for dir1 in main_dir: if not os.path.isdir(dir1): for

run os.system commands on a new terminal- Python 3

倾然丶 夕夏残阳落幕 提交于 2019-12-13 04:00:21
问题 I am running a program which allows me to run terminal commands through my Python code which takes input from the user through the command line. This is the part of the code where I open Google-Chrome import sys import os os.system("google-chrome") #I have Ubuntu 16.04 It opens the browser but the problem is that the terminal on which my python code is running becomes the same as the one where Chrome is running which means that I cannot give further input to my Python code. To solve this

Not able to execute terminal command(top) in python

僤鯓⒐⒋嵵緔 提交于 2019-12-11 15:55:14
问题 I have this command: top -d 1.0 -n 1| grep Mem when i execute it in terminal i get: KiB Mem : 16330216 total, 3902792 free, 10619512 used, 1807912 buff/cache KiB Swap: 8509436 total, 4584448 free, 3924988 used. 4848204 avail Mem I want to save this output in a text file. I want to keep a track of this every 10 seconds. Therefore i tried: def repeat(): print(time.ctime()) threading.Timer(10, repeat).start() f= open('ss.txt', 'w') top= os.system("sudo top -d 1.0 -n 1| grep Mem") s=str(top) text

How to delete a specific number of files from many folders?

隐身守侯 提交于 2019-12-11 14:40:54
问题 Let's suppose that I have a set of folders. I each folder I have more than 1000 files, I need to count 1000 in each folder than delete the rest, For example: Folder1 contains 1234 numpy files, I want to keep 1000 and delete the 234 files. I use python, I display number of files per folder, but I can't keep only 1000 files and delete the rest. import os b=0 for b in range(256): path='path.../traces_classes_Byte_0/Class_Byte_Hypothesis_'+str(b) num_files = sum(os.path.isfile(os.path.join(path,

os.popen().read() - charmap decoding error

試著忘記壹切 提交于 2019-12-11 03:42:56
问题 I have already read UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to <undefined>. While the error message is similar, the code is completely different, because I use os.popen in this question, not open . I cannot use the answers from the other questions to solve this problem. output = os.popen("dir").read() This line, which is supposed to assign the output of command "dir" to variable "output", is causing this error: 'charmap' codec can't decode byte

Python restart program

半腔热情 提交于 2019-12-10 12:25:19
问题 I made a program that asks you at the end for a restart. I import os and used os.execl(sys.executable, sys.executable, * sys.argv) but nothing happened, why? Here's the code: restart = input("\nDo you want to restart the program? [y/n] > ") if str(restart) == str("y"): os.execl(sys.executable, sys.executable, * sys.argv) # Nothing hapens else: print("\nThe program will be closed...") sys.exit(0) 回答1: import os import sys restart = input("\nDo you want to restart the program? [y/n] > ") if

How to create directories and sub directories efficiently and elegantly in Python 2.7?

江枫思渺然 提交于 2019-12-06 00:26:23
I am trying to create a bunch of directories and sub directories at a specific location in my PC. My process is something like this: Check if there's any directory with the same directory name. Skip if so. If not, create the directory and the pre-defined sub directories under that directory. This is the code I came up with using os module: def Test(): main_dir = ["FolderA", "FolderB"] common_dir = ["SubFolder1", "SubFolder2", "SubFolder3"] for dir1 in main_dir: if not os.path.isdir(dir1): for dir2 in common_dir: os.makedirs("%s/%s" %(dir1,dir2)) I am wondering if there's any better way to do

Is it safe to use os.environ.setdefault?

…衆ロ難τιáo~ 提交于 2019-12-04 15:37:29
问题 From my ipython shell, I see a method setdefault in os.environ but it is not documented. http://docs.python.org/library/os.html#os.environ. Is it documented somewhere else? def setdefault(self, key, failobj=None): if key not in self: self[key] = failobj return self[key] Can I use this function or write a wrapper for those lines? 回答1: The os.environ documentation does state it's a mapping: A mapping object representing the string environment. As such it behaves according to the python mapping