2

I'm using canopy python. The shell has commands "pwd" and "cd". It even does autocomplete. Is there a command - not something I have to write and keep track of - a built-in command that just lists the files in the directory. I've tried ls, ls(), dir, dir(), directory, directory(), files, files(), filelist, filelist().

I've searched their documentation for listing files. It shows how to get a list of files in a python program - that's great. I do that all the time. I don't want to do that every time I want to list the contents of the current directory.

Is there a built-in shell command for this?

elbillaf
  • 1,952
  • 10
  • 37
  • 73

4 Answers4

3

You can try

import os
fileList = os.listdir(directory_path)

so that you have the list of files in the python program to manipulate.

If you want the list of files in the current working directory in the way that ls would give you you can do.

fileList = os.listdir(os.getcwd())

If you really want a short command to type over and over you can write

ls = lambda : os.listdir(os.getcwd())

and then it will run this command when you type

ls()
Jon Deaton
  • 3,943
  • 6
  • 28
  • 41
  • This works. I already know how to do this. This is irritating to type at the shell. It seems incredible to me they don't have a built-in command to do this. – elbillaf Aug 04 '17 at 19:11
  • Try `ls = lambda : os.listdir(os.getcwd())` and then you can just run it with `ls()` – Jon Deaton Aug 04 '17 at 19:14
  • That's good. I'm guessing this is as good as it gets. Is there a way to script my canopy so this runs every time I invoke canopy? – elbillaf Aug 04 '17 at 19:17
1

You can use the python built in module, os, to query a directory using the system method. Please see below.

import os
os.system("dir")

Update

Please keep in mind that my solution is OS specific (i.e. ls vs dir) so Jon's solution is preferable.

Eric
  • 489
  • 5
  • 16
  • Neither os.system("dir") nor os.system("ls") seems to work for me, though I'm running windows 7. – elbillaf Aug 04 '17 at 19:14
  • Strange, i'm running windows 7 and it works for me. I'm not sure, but like I said, using os.listdir(os.getcwd()) is definitely the better solution. – Eric Aug 04 '17 at 19:16
1

The premise of the question seems inconsistent. If you can do the ipython pwd or cd standard magic commands, then you should also be able to access the ipython standard alias ls. If you cannot, then it almost certainly because you've overwritten / shadowed the meaning of the ls alias.

It's crucial to distinguish between (1) a plain Python shell (opened by typing python at a Canopy command prompt), which does not provide pwd nor cd nor 'lscommands; and (2) *any* IPython shell, whether the Python panel in the Canopy GUI or anipython` terminal in the Canopy Command Prompt, which does provide those, and many other Ipython "magic" commands.

(For more about IPython magic commands, see http://ipython.readthedocs.io/en/stable/interactive/magics.html; these are the docs for IPython version 6, but they are mostly the same for IPython version 5 which Canopy uses in order to support both Python 2.7 and Python 3.5+.)

The two previous answers are always correct in that they will work in any Python program or shell (depending on OS). That is because they only use the Python standard library. However they are not analogous to the cd and pwd commands that you mention, which are specific to the IPython shell (not to programs running in an IPython shell).

IMO, there is very seldom a good reason for opening a plain Python shell. If you simply want to run a script file from a Command Prompt, then by all means python myscript.py. But if you want an interactive shell, IPython provides so much extra capability that it has been for many years the de facto standard for Python shells (which is why it is used for the Canopy GUI's Python shell).

Jonathan March
  • 5,800
  • 2
  • 14
  • 16
0

Got a note back from Canopy Support). There is an environment variable, COMSPEC that was set to:

C:\windows\system32\cmd.exe; 

That semicolon at the end is wrong. This is a single directory and not a list of directories. I removed that semicolon and suddenly the 'ls' command works as I remember!

elbillaf
  • 1,952
  • 10
  • 37
  • 73