Automatically creating a list in Python [duplicate] - python

This question already has answers here:
How do I list all files of a directory?
(21 answers)
Closed 8 years ago.
My code:
red_images = 'DDtest/210red.png'
green_images = 'DDtest/183green.png'
b = [red_images, green_images]
shuffle(b)
I have several hundred pictures, and in hopes of making my code as concise as possible (and to expand my python knowledge), I was wondering how I write code that automatically takes the files in a folder and makes them a list.
I have done similar things in R, but I'm not sure how to do it in python.

You can also use os:
import os
from random import shuffle
# Base directory from which you want to search
base_dir = "/path/to/whatever"
# Only take the files in the above directory
files = [f for f in os.listdir(base_dir)
if os.path.isfile(os.path.join(base_dir, f))]
# shuffle (in place)
shuffle(files)

import glob
my_new_list = glob.glob("/your/folder/*")

Related

Reading multiple CSVs using glob results in wrong order [duplicate]

This question already has answers here:
Sort filenames in directory in ascending order [duplicate]
(3 answers)
Non-alphanumeric list order from os.listdir()
(14 answers)
Closed 2 years ago.
I need to read in multiple CSV files in the correct order. The files are named with sequential numbers, like "file_0.csv", "file_1.csv", "file_2.csv", ... and were created in this same order.
When running my code, the files are not kept in this order but instead completely mixed up.
There are no other files in the path folder.
path = "stored_files"
filenames = glob.iglob(path + "/*.csv")
dataframes = []
for filename in filenames:
dataframes.append(pd.read_csv(filename))
AFAIK, glob provides randomly-ordered access to the folder. You can always sort fhe filenames:
path = "stored_files"
filenames = glob.iglob(path + "/*.csv")
dataframes = []
for filename in sorted(filenames):
dataframes.append(pd.read_csv(filename))
String sort will not sort strings with numbers the way you expect (in particular, 10 precedes 2). So, if you know what your filenames look like, do loop through the numbers and append "foo"+str(i)+".csv" or whatever to your filelist.

Python list different directory without chdir [duplicate]

This question already has answers here:
How do I list all files of a directory?
(21 answers)
Closed 3 years ago.
I have a directory with my .py file and I have another directory where I am writing files to. How can I get a os.listdir of the second directory without using os.chdir?
Edit: Sorry seems like there are 2 parts to my implementation. My current code is this:
files = [f for f in os.listdir(2nd_d) if os.path.isfile(f)]
It currently returns an empty list when there are some files in the directory.
Just use the full path of that other directory.
dir1_contents = os.listdir('C://path/to/my/dir1')
dir2_contents = os.listdir('C://path/to/my/dir2')
Why not just do a full path check?
import numpy as np
import os
base = 'C:/Users/user/desktop/files/'
for i in range(50):
temp = np.random.normal(0, 1, (100, 100))
np.save(f'{base}File_{i}.npy', temp)
files = os.listdir(base)

How to create and write to files indefinitely in Python? [duplicate]

This question already has answers here:
Implement touch using Python?
(16 answers)
Closed 4 years ago.
I have to make a script which creates a certain amount of subfolders in each main folder (dir1, dir2, and dir3). Then, inside of each subfolder, there has to be files (.txt.for example) been created constantly until I (the user) decides to stop the program. Right now, I can create subfolders (from 0 to 99) in each main folder, but I'm not sure how to create the .txt files. Any suggestions or ideas would be very much appreciated. Here's my code:
import os
folders = ["dir1", "dir2", "dir3"]
count = 0
for folder in folders:
for count in range(100):
subfolder = str(count)
newfolder = os.makedirs(os.path.join(folder, subfolder))
I'm trying to do something like this
with open("%s/01.txt" %count, "wa") as fh
so it goes to each subdirectory and create the file
You can simply open and write nothing. For example:
with open("01.txt", "w") as fh:
fh.write("")
This means that you can also write stuff in the future, if necessary. It may not be necessary to .write(), but it improves readability.

How to create directory in ram and get path to it? [duplicate]

This question already has answers here:
How can I create a ramdisk in Python?
(4 answers)
Closed 3 years ago.
I'm looking for a library that allows to create a "fake" directory in RAM and get path to it that would work like a path to normal directory on disk.
My scenario is that I have a python script that executes another program (third party that I cannot modify) but it produces files and writes them to a specified location.
Then, in my script, I read these files and do something with them.
It's slow and I know that the bottleneck here is reading/writing files to/from disk (even if it's SSD).
Is it possible that I don't change the core of my script and only replace the temporary folder path that I use to store intermediate files?
I don't need them and I remove them after they are processed.
The perfect solution would be something like this:
import fakeRAM
tmp_dir_path = fakeRAM.get_path()
...
os.system("program.exe " + tmp_dir_path)
import tempfile
import pandas as pd
import io
data=pd.read_csv("data.csv")
temp = tempfile.NamedTemporaryFile(prefix="", suffix=".csv", mode='w+b', delete=False)
data.to_csv(temp.name)
print("The created file is", temp.name)

Make a List for sub directories path [duplicate]

This question already has answers here:
Getting a list of all subdirectories in the current directory
(34 answers)
Closed 7 years ago.
I have a directory and I need to make a path list for the sub directories.
For example my main directory is
C:\A
which contains four different sub directories : A1,A2,A3,A4
I need a list like this:
Path_List = ["C:\A\A1","C:\A\A2","C:\A\A3","C:\A\A4"]
Cheers
import os
base_dir = os.getcwd()
sub_dirs = [os.path.join(base_dir, d) for d in os.listdir(base_dir)]
You could (and should, i think) use os module, wich is easy to use and provides a lot of stuff to deal with paths.
I wont say anymore, because your question is vague and shows no effort on searching. You are welcome!!
sub_dirs = os.listdir(os.getcwd()) ## Assuming you start the script in C:/A
path_list = []
for i in sub_dirs:
path_list.append(os.path.join(os.getcwd(),i))
Dirty and fast.

Categories