-1

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.

Vega
  • 27,856
  • 27
  • 95
  • 103
weasel
  • 534
  • 1
  • 5
  • 18
  • If you are able to use glob, I like to use that. `Fls = glob.glob(file_path)`. – Kyle Jan 27 '20 at 22:46
  • Answer, since question got closed: make references for both inputs ```files = [f for f in os.listdir(2nd_d) if os.path.isfile(2nd_d+'/'+f)]``` – weasel Feb 25 '20 at 11:41

2 Answers2

0

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')
Mark Snyder
  • 1,635
  • 3
  • 12
0

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)
Will.Evo
  • 1,112
  • 13
  • 31