我在用C或FORTRAN代码理解python时遇到了一些麻烦。你知道吗
我有一个Python脚本,它简单地部署了两个线程,每个线程将进入一个子目录并调用相同的Fortran程序(为了简单起见,使用操作系统,但波本是一样的。我还包括一个C程序来显示相同的问题)
当调用a.out(F77或C)的每个实例时,我都希望它们知道它们在哪个CWD中,但是10次或20次运行中有1次不知道,所以我认为我的python有问题。你知道吗
使用F77代码运行Python脚本时出现以下错误:
CWD_F: /home/usr/f_fail/2
CWD_F: /home/usr/f_fail/2
Fortran runtime error: Cannot open file 'dummy.txt': File exists
当我和C一起跑步时,有时我会
CWD_C : /home/usr/f_fail/2
CWD_C : /home/usr/f_fail/2
这是我非常简单的python。有一件事;当我取消注释时间。睡眠() 看来效果不错!你知道吗
#!/usr/bin/python
import threading, os,time
def t1( newDir ):
os.chdir( newDir )
os.system('../a.out')
os.chdir('..')
def t2( newDir ):
os.chdir( newDir )
os.system('../a.out')
os.chdir('..')
#main starts here
cwd_=os.getcwd()
try :
os.mkdir('1')
except:
pass
try :
os.mkdir('2')
except:
pass
newDir1=os.path.join( cwd_ , '1')
newDir2=os.path.join( cwd_ , '2')
tt1 = threading.Thread( target=t1 , args=(newDir1,))
tt2 = threading.Thread( target=t2 , args=(newDir2,))
tt1.start()
#time.sleep(1)
tt2.start()
tt1.join()
tt2.join()
下面是FORTRAN77代码:
program test
implicit none
integer i,k,unt
real*8 a(3,10 ), c(3)
character*132 fl,cwdpath
do i=1,10
c(1)=i*1.11
c(2)=i*2.22
c(3)=i*3.33
a(:,i)=c
enddo
unt=1
fl='dummy.txt'
call getcwd(cwdpath)
write(*,*) 'CWD_F: ', cwdpath
write(*,*) 'unit: ', unt
open(unt,file=fl,status='unknown')
close(unt,status='delete')
open(unt,file=fl,status='new')
do k=1,10
write(unt,200) a(1,k),a(2,k),a(3,k)
enddo
close(unt)
200 format(' ',F10.4,F10.4,F10.4)
end
或者,这是我的C代码:
#include <unistd.h>
#include <stdio.h>
#define PATH_MAX_ 1024
int main() {
char cwd[PATH_MAX_];
if (getcwd(cwd, sizeof(cwd)) != NULL)
fprintf(stdout, "CWD_C : %s\n", cwd);
return 0;
}
任何帮助或想法都将不胜感激。 干杯!! 朱莉娅
CWD是进程范围的,所以发生的情况如下:首先,执行其中一个线程中的
os.chdir()
,将进程切换到该目录。然后,执行另一个线程中的os.chdir()
,将进程切换到另一个目录。然后运行线程中的其余代码。您需要序列化线程操作以使代码正常工作。你知道吗或者,您可以告诉shell在运行程序之前更改目录;由于每个shell是一个单独的进程,因此没有冲突的目录更改,而且以后也不需要返回父目录。你知道吗
由于已将路径传递给可执行文件,因此打开文件时只需使用路径:
相关问题 更多 >
编程相关推荐