GAMS中跨平台文件系统操作的最佳实践 [英] Best practice for cross-platform file system manipulations in GAMS

查看:494
本文介绍了GAMS中跨平台文件系统操作的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一些GAMS代码操作文件系统。我正在中途来到这个项目,作为GAMS的新手,但我习惯用其他语言编写跨平台的文件操作。这个代码是跨平台的Linux和Windows应该是好的。现在有一个注释行
$ b $ $ p $ * $ call'mkdir%runfolder%'
code>

我想取消注释(启用),但是,我很难找到文档是否可以工作跨平台(我不怀疑),如果不是,我怎样才能以跨平台的方式做到这一点。



如何可以操纵文件系统,保证跨平台工作?


<解决方案尝试使用定义文件分隔符的全局变量。您可以通过查询system.filesys系统变量并相应地设置值来做到这一点。如:

  * -----特定于平台的调整
*设置用于相对路径名的文件分隔符
$ iftheni%system.filesys%== DOS $ setglobal filesep\
$ elseifi%system.filesys%== MS95 $ setglobal filesep\
$ elseifi%system .filesys%== MSNT $ setglobal filesep\
$ else $ setglobal filesep/
$ endif

然后你可以在需要指定路径的时候使用它,例如:

  * -----设置数据和输出目录
*默认查找兄弟目录data中的数据
$如果没有设置data_dir $ setglobal data_dir ..%filesep%data%filesep%

请注意,GAMS不会让您缩进 $ if 和相关指令。


I would like to manipulate the file system from some GAMS code. I am coming to this project mid-stream, and as a GAMS newbie, but am used to writing cross-platform file manipulations in other languages. It would be good for this code to be cross-platform Linux and Windows. Right now there is a commented-out line

*$call 'mkdir "%runfolder%"'

that I would like to un-comment (enable), however, I am having a hard time finding documentation on whether this would work cross-platform (I suspect not), and if not, how I could do this in a cross-platform way.

How can I manipulate the file system in a way that is guaranteed to work cross-platform?

解决方案

Try using a global variable that defines the file separator. You can do this by querying the system.filesys system variable and setting the value accordingly. Such as:

* ----- Platform Specific Adjustments
* Setup the file separator to use for relative pathnames 
$iftheni %system.filesys% == DOS $setglobal filesep "\"
$elseifi %system.filesys% == MS95 $setglobal filesep "\"
$elseifi %system.filesys% == MSNT $setglobal filesep "\"
$else $setglobal filesep "/"
$endif

Then you can use this when you need to specify paths, as in:

* ----- Set data and output directories
* By default look for data in the sibling directory "data"
$if NOT set data_dir    $setglobal data_dir ..%filesep%data%filesep%   

Note that GAMS doesn't let you indent $if and related directives.

这篇关于GAMS中跨平台文件系统操作的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆