我正在使用crontab为我的minecraft服务器运行一个维护脚本。大多数情况下,它工作得很好,除非crontab尝试使用重新启动脚本。如果我手动运行重新启动脚本,则不会出现任何问题。因为我相信它与路径名有关,所以我试图确保它总是在执行来自minecraft目录的任何minecraft命令。因此,我将命令封装在pushd/popd中:
os.system("pushd /directory/path/here")
os.system("command to sent to minecraft")
os.system("popd")下面是一个互动会话,将“我的世界”从等式中剔除。一个简单的"ls“测试。正如您所看到的,它根本不从point.Clearly目录运行os.system命令,而是从/etc/目录运行,这是我运行python的目录,以说明我的python pushd不能通过python工作,所以我想知道我还能如何实现这一点。谢谢!
>>> def test():
... import os
... os.system("pushd /home/[path_goes_here]/minecraft")
... os.system("ls")
... os.system("popd")
...
>>> test()
~/minecraft /etc
DIR_COLORS cron.weekly gcrypt inputrc localtime mime.types ntp ppp rc3.d sasldb2 smrsh vsftpd.ftpusers
DIR_COLORS.xterm crontab gpm-root.conf iproute2 login.defs mke2fs.conf ntp.conf printcap rc4.d screenrc snmp vsftpd.tpsave
X11 csh.cshrc group issue logrotate.conf modprobe.d odbc.ini profile rc5.d scsi_id.config squirrelmail vz
adjtime csh.login group- issue.net logrotate.d motd odbcinst.ini profile.d rc6.d securetty ssh warnquota.conf
aliases cyrus.conf host.conf java lvm mtab openldap protocols redhat-release security stunnel webalizer.conf
alsa dbus-1 hosts jvm lynx-site.cfg multipath.conf opt quotagrpadmins resolv.conf selinux sudoers wgetrc
alternatives default hosts.allow jvm-commmon lynx.cfg my.cnf pam.d quotatab rndc.key sensors.conf sysconfig xinetd.conf
bashrc depmod.d hosts.deny jwhois.conf mail named.caching-nameserver.conf passwd rc rpc services sysctl.conf xinetd.d
blkid dev.d httpd krb5.conf mail.rc named.conf passwd- rc.d rpm sestatus.conf termcap yum
cron.d environment imapd.conf ld.so.cache mailcap named.rfc1912.zones pear.conf rc.local rsyslog.conf setuptool.d udev yum.conf
cron.daily exports imapd.conf.tpsave ld.so.conf mailman netplug php.d rc.sysinit rwtab shadow updatedb.conf yum.repos.d
cron.deny filesystems init.d ld.so.conf.d makedev.d netplug.d php.ini rc0.d rwtab.d shadow- vimrc
cron.hourly fonts initlog.conf libaudit.conf man.config nscd.conf pki rc1.d samba shells virc
cron.monthly fstab inittab libuser.conf maven nsswitch.conf postfix rc2.d sasl2 skel vsftpd
sh: line 0: popd: directory stack empty=== (带有Python2.4的CentOS服务器)
发布于 2011-06-01 06:58:53
每个shell命令都在单独的进程中运行。它产生一个shell,执行pushd命令,然后shell退出。
只需在相同的shell脚本中编写命令:
os.system("cd /directory/path/here; run the commands")一种更好的(也许)方法是使用subprocess模块:
from subprocess import Popen
Popen("run the commands", shell=True, cwd="/directory/path/here")发布于 2012-12-13 04:11:23
在Python 2.5及更高版本中,我认为更好的方法是使用上下文管理器,如下所示:
import contextlib
import os
@contextlib.contextmanager
def pushd(new_dir):
previous_dir = os.getcwd()
os.chdir(new_dir)
try:
yield
finally:
os.chdir(previous_dir)然后,您可以像下面这样使用它:
with pushd('somewhere'):
print os.getcwd() # "somewhere"
print os.getcwd() # "wherever you started"通过使用上下文管理器,您将获得异常和返回值的安全:即使您抛出异常或从上下文块内部返回,您的代码也将始终返回到它开始的位置。
您还可以在嵌套块中嵌套pushd调用,而不必依赖全局目录堆栈:
with pushd('somewhere'):
# do something
with pushd('another/place'):
# do something else
# do something back in "somewhere"发布于 2012-05-10 13:56:16
pushd和popd有一些额外的功能:它们将以前的工作目录存储在堆栈中-换句话说,你可以pushd五次,做一些事情,然后popd五次,直到你开始的地方。您不会在这里使用它,但它可能对其他搜索类似问题的人有用。以下是您可以模拟它的方式:
# initialise a directory stack
pushstack = list()
def pushdir(dirname):
global pushstack
pushstack.append(os.getcwd())
os.chdir(dirname)
def popdir():
global pushstack
os.chdir(pushstack.pop())https://stackoverflow.com/questions/6194499
复制相似问题