Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7763365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:38:27+00:00 2026-06-01T14:38:27+00:00

Suppose I have a directory A, and subdirectory B. I cd into A and

  • 0

Suppose I have a directory A, and subdirectory B. I cd into A and launch lisp. In that lisp process, I would like to launch a Python subprocess where Python sees B as its current working directory. The lisp process needs to have cwd in A, and the python process should have cwd in B. How do I do this in a cross-platform, simple way?

I’m looking for a solution that works with CCL and SBCL (probably using ‘run-program function), and works for Windows, Linux, and OS X.

I looked at the CCL run-program documentation, and I didn’t see a way to change the cwd of the launched process.

I looked at Python command-line arguments, and I didn’t see one that would change the cwd of the python process.

I thought about a run-program call for ‘cd B; python …’, but I’m not sure how that would work, since it’s really running two programs; cd, and then python.

The Python code is being provided as input (as a file), so I cannot change any of that code (by adding an os.chdir() call or similar).

A python wrapper file that launches the python input file as a subprocess isn’t ideal, because I’m sending stdin and listening to stdout of the python process launched by lisp. Adding another subprocess in between lisp and the python process that evals the input file means I’d need to do a lot of stout/stdin relaying, and I have a feeling that this would be brittle.

krzysz00’s approach worked very well. Since the directory change is handled in lisp, before the python process is launched, this approach will work for launching other processes in different subdirectories (not just python).

For documentation, here’s my code using krzsz00’s approach that worked for SBCL & CCL. Note that it uses Hoyte’s defmacro! macro, from Let Over Lambda, to easily avoid unwanted variable capture:

#+:SBCL
(defun cwd (dir)
  (sb-posix:chdir dir))

(defun getcwd ()
  #+SBCL (sb-unix:posix-getcwd)
  #+CCL (current-directory))

(defmacro! with-cwd (dir &body body)
  `(let ((,g!cwd (getcwd)))
     (unwind-protect (progn
                       (cwd ,dir)
                       ,@body)
     (cwd ,g!cwd))))

Usage:

(with-cwd "./B"
  (run-program ...))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:38:28+00:00Added an answer on June 1, 2026 at 2:38 pm

    To run external programs (like your python process portably) see external-program. To change the current working directory, use this slightly modified (public domain) function cwd from the file http://files.b9.com/lboot/utils.lisp, which is reproduced below.

    (defun cwd (&optional dir)
      "Change directory and set default pathname"
      (cond
       ((not (null dir))
        (when (and (typep dir 'logical-pathname)
               (translate-logical-pathname dir))
          (setq dir (translate-logical-pathname dir)))
        (when (stringp dir)
          (setq dir (parse-namestring dir)))
        #+allegro (excl:chdir dir)
        #+clisp (#+lisp=cl ext:cd #-lisp=cl lisp:cd dir)
        #+(or cmu scl) (setf (ext:default-directory) dir)
        #+cormanlisp (ccl:set-current-directory dir)
        #+(and mcl (not openmcl)) (ccl:set-mac-default-directory dir)
        #+openmcl (ccl:cwd dir)
        #+gcl (si:chdir dir)
        #+lispworks (hcl:change-directory dir)
        #+sbcl (sb-posix:chdir dir)
        (setq cl:*default-pathname-defaults* dir))
       (t
        (let ((dir
           #+allegro (excl:current-directory)
           #+clisp (#+lisp=cl ext:default-directory #-lisp=cl lisp:default-directory)
           #+(or cmu scl) (ext:default-directory)
           #+sbcl (sb-unix:posix-getcwd/)
           #+cormanlisp (ccl:get-current-directory)
           #+lispworks (hcl:get-working-directory)
           #+mcl (ccl:mac-default-directory)
           #-(or allegro clisp cmu scl cormanlisp mcl sbcl lispworks) (truename ".")))
          (when (stringp dir)
        (setq dir (parse-namestring dir)))
          dir))))
    

    Combining these two functions, the code you want is:

    (cwd #p"../b/")
    (external-program:start "python" '("file.py") :output *pythins-stdout-stream* :input *pythons-stdin-stream*)
    (cwd #p"../a/")
    

    This will cd to B, run the python process as if by python file.py &, send the python process’s stdin/stdout to the specified streams (look at the external-program documentation for more details), and finally execute another cwd that returns the lisp process to A. If the lisp process should wait until the python process is finished, use external-program:run instead of external-program:start.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a directory which I would like to be the default for Python
Suppose I have a tar that contains: / # Root directory /level1/ # A
For instance, I suppose I have a directory that contains the following folders foo_bar
I am getting trouble with directory listing.Suppose, I have a directory with some subdirectory(named
Suppose I have a directory structure like the following /a/b/testB.xml /a/c/testC.xml /a/testD.xml and I
Suppose I have a directory look like: ABC |_ a1.txt |_ a2.txt |_ a3.txt
Suppose you have a directory structure like this: A/ B/ a.1 b.2 c.3 I'm
Suppose I have a directory structure like so /var/www └── test.js Probably the most
Suppose I have a directory /dir inside which there are 3 symlinks to other
Suppose I have the following directory layout in a Maven project: src/ |-- main

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.