小编Jah*_*hid的帖子

制作目录时出现Python"FileExists"错误

我在集群系统上有几个并行运行的线程.每个python线程输出到一个目录mydir.每个脚本在输出检查之前是否存在mydir,如果不存在则创建它:

if not os.path.isdir(mydir):
    os.makedirs(mydir)
Run Code Online (Sandbox Code Playgroud)

但这会产生错误:

os.makedirs(self.log_dir)                                             
  File "/usr/lib/python2.6/os.py", line 157, in makedirs
mkdir(name,mode)
OSError: [Errno 17] File exists
Run Code Online (Sandbox Code Playgroud)

我怀疑这可能是由于竞争条件,一个工作在另一个工作之前创建了dir.这可能吗?如果是这样,如何避免这种错误?

我不确定这是一个竞争条件,所以想知道Python中的其他问题是否会导致这个奇怪的错误.

python filesystems queue file-io cluster-computing

37
推荐指数
3
解决办法
5万
查看次数

条件表达式中的语法错误:意外标记`;'

我有一个shell脚本,应该接受多个参数.

它可以接受参数"update"或"create".如果没有传递参数,则用户应该收到错误.但是,在构建我的if/elif条件时,我收到错误:

syntax error in conditional expression: unexpected token `;'
Run Code Online (Sandbox Code Playgroud)

代码:

firstParam=$1
echo $firstParam //update/create/{empty}

if [[ "$firstParam" == "" ]]; then
    printf "${RED}Use this script as \"tzfrs update/new [projectName]\"${NC} \n"
    exit 1
elif [[ "$firstParam" == "update"]]; then
  printf "update"
  exit 1
fi
Run Code Online (Sandbox Code Playgroud)

如果我有这样的脚本

if [[ "$firstParam" == "" ]]; then
    printf "${RED}Use this script as \"tzfrs update/new [projectName]\"${NC} \n"
    exit 1
fi
Run Code Online (Sandbox Code Playgroud)

错误处理工作,我看到以下消息

Use this script as "tzfrs update/new [projectName]"

但是,添加elif条件时我得到了上述错误.有人有什么想法?

bash shell if-statement

22
推荐指数
1
解决办法
1万
查看次数

使用shell脚本中的read命令逐行读取输入文件会跳过最后一行

我通常使用read命令逐行读取shell脚本的输入文件.如果未在输入文件blah.txt中的最后一行末尾插入新行,则下面的示例代码会产生错误的结果.

#!/bin/sh

while read line
do
echo $line
done <blah.txt
Run Code Online (Sandbox Code Playgroud)

因此,如果输入文件读取类似于 -

One 
Two
Three
Four
Run Code Online (Sandbox Code Playgroud)

我四点后没有回复,脚本无法读取最后一行,并打印出来

One
Two
Three
Run Code Online (Sandbox Code Playgroud)

现在,如果我在四个之后留下一个额外的空白行,比如,

One 
Two
Three
Four
//blank line
Run Code Online (Sandbox Code Playgroud)

输出打印所有行,包括四行.但是,当我使用cat命令读取一行时,情况并非如此; 包括最后一行在内的所有行都打印出来,而不必在末尾添加额外的空白行.

有人知道为什么会这样吗?我创建的脚本主要由其他人运行,因此没有必要在每个输入文件的末尾添加额外的空行.

我一直试图解决这个问题; 如果你有任何解决方案我会很感激(当然,cat命令是一个,但我想知道阅读不起作用的原因).

bash shell file-io parsing

20
推荐指数
3
解决办法
11万
查看次数

使用GCM向多个Android设备发送推送通知

我正在关注http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/?通过GCM发送推送通知.一切正常,但我能够将推送通知发送到一个设备.注册其他设备会替换先前设备的注册ID.我试过Shardool在http://javapapers.com/android/android-multicast-notification-using-google-cloud-messaging-gcm/中提供的解决方案?但它不起作用.

任何建议都会有很大帮助.

以下是我的gcm.php代码,用于注册设备并发送推送通知,但仅限于最近注册的单个设备.

gcm.php

<?php
//generic php function to send GCM push notification
function sendPushNotificationToGCM($registatoin_ids, $message) {
    //Google cloud messaging GCM-API url
    $url = 'https://android.googleapis.com/gcm/send';
    $fields = array(
        'registration_ids' => $registatoin_ids,
        'data' => $message,
    );
    // Google Cloud Messaging GCM API Key
    define("GOOGLE_API_KEY", "MY_KEY");       
    $headers = array(
        'Authorization: key=' . GOOGLE_API_KEY,
        'Content-Type: application/json'
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = …
Run Code Online (Sandbox Code Playgroud)

php notifications android google-cloud-messaging

20
推荐指数
2
解决办法
7万
查看次数

如何将类名的一部分定义为宏?

例如,如果我有这么多类在同一平台上具有相同的前缀:

在android中:

Printer *p=new AndroidPrinter();
Writer *w=new AndroidWriter();
Connector *c=new AndroidConnector();
Run Code Online (Sandbox Code Playgroud)

在iOS中:

Printer *p=new IOSPrinter();
Writer *w=new IOSWriter();
Connector *c=new IOSConnector();
Run Code Online (Sandbox Code Playgroud)

可以像这样定义类名的一部分:

#define PREFIX Android

int main(){
    Printer *p=new PREFIXPrinter();
    Writer *w=new PREFIXWriter();
    Connector *c=new PREFIXConnector();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

代替:

#define PLATFORM 0

#if PLATFORM==0
#define PRINTER AndroidPrinter
#else
#define PRINTER IOSPrinter
#endif

#if PLATFORM==0
#define WRITER AndroidWriter
#else
#define WRITER IOSWriter
#endif

#if PLATFORM==0
#define CONNECTOR AndroidConnector
#else
#define CONNECTOR IOSConnector
#endif

int main(){
    Printer *p=new PRINTER();
    Writer *w=new …
Run Code Online (Sandbox Code Playgroud)

c++

16
推荐指数
3
解决办法
2925
查看次数

将多个目录中的文件重命名为目录名称

我有这样的事情:

v_1/file.txt
v_2/file.txt
v_3/file.txt
...
Run Code Online (Sandbox Code Playgroud)

我想将这些文件重命名为以下内​​容:

v_1.txt
v_2.txt
v_3.txt
...
Run Code Online (Sandbox Code Playgroud)

在同一目录中.

我想我可以使用,rename但我无法弄清楚如何同时使用它与文件夹和文件重命名.

linux rename batch-rename

14
推荐指数
1
解决办法
2万
查看次数

错误:使用已删除的函数bool regex_match和gcc 5.2.0

使用GCC 4.9.2编译的代码甚至没有任何警告,但在GCC 5.2.0中显示以下错误:

error: use of deleted function ‘bool std::regex_match(const std::__cxx11::basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&, std::__cxx11::match_results<typename std::__cxx11::basic_string<_Ch_type, _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&, const std::__cxx11::basic_regex<_Ch_type, _Rx_traits>&, std::regex_constants::match_flag_type) [with _Ch_traits = std::char_traits<char>; _Ch_alloc = std::allocator<char>; _Alloc = std::allocator<std::__cxx11::sub_match<__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> > > >; _Ch_type = char; _Rx_traits = std::__cxx11::regex_traits<char>; typename std::__cxx11::basic_string<_Ch_type, _Ch_traits, _Ch_alloc>::const_iterator = __gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >]’
     if(std::regex_match(toString(index),result,re)){index=fabs(index);}
Run Code Online (Sandbox Code Playgroud)

抛出错误的一段代码:

bool negative_flag=false;
std::regex re("-[^-]*");
std::smatch result;
if(std::regex_match(toString(index),result,re)){index=fabs(index);}
Run Code Online (Sandbox Code Playgroud)

错误if在行中.可能是什么导致了这个?

c++ gcc g++ c++11

14
推荐指数
1
解决办法
3002
查看次数

如何将正则表达式中的方括号与grep匹配?

我试图匹配两者[]grep,但只是成功匹配[.无论我如何尝试,我似乎无法正确匹配].

这是一个代码示例:

echo "fdsl[]" | grep -o "[ a-z]\+" #this prints fdsl
echo "fdsl[]" | grep -o "[ \[a-z]\+" #this prints fdsl[
echo "fdsl[]" | grep -o "[ \]a-z]\+" #this prints nothing
echo "fdsl[]" | grep -o "[ \[\]a-z]\+" #this prints nothing
Run Code Online (Sandbox Code Playgroud)

编辑:我需要这样做的原始正则表达式是这样的:

echo "fdsl[]" | grep -o "[ \[\]\t\na-zA-Z\/:\.0-9_~\"'+,;*\=()$\!@#&?-]\+" 
#this prints nothing
Run Code Online (Sandbox Code Playgroud)

注意:我已经尝试过这篇文章中的所有答案,但这不适用于这个特殊情况.我需要在里面使用这些括号[].

regex bash grep

13
推荐指数
2
解决办法
1万
查看次数

如何在C中按顺序对数字和字母排序文件名?

我使用以下代码按字母顺序对文件进行排序,并对文件进行排序,如图所示:

for(int i = 0;i < maxcnt;i++) 
{
    for(int j = i+1;j < maxcnt;j++)
    {           
        if(strcmp(Array[i],Array[j]) > 0)
        {            
            strcpy(temp,Array[i]);      
            strcpy(Array[i],Array[j]);      
            strcpy(Array[j],temp);    
        }    
    } 
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但我需要按照Windows资源管理器中的顺序对其进行排序

在此输入图像描述

怎么这样排序?请帮忙

c c++ sorting

12
推荐指数
4
解决办法
6980
查看次数

在Unix shell中反转一个文件

我有一个文件parse.txt

parse.txt包含以下内容

remo/hello/1.0,remo/hello2/2.0,remo/hello3/3.0,whitney/hello/1.0,julie/hello/2.0,julie/hello/3.0
Run Code Online (Sandbox Code Playgroud)

我想使用parse.txt将output.txt文件作为(从最后一个到第一个的顺序)

julie/hello/3.0,julie/hello/2.0,whitney/hello/1.0,remo/hello3/3.0,remo/hello2/2.0,remo/hello/1.0
Run Code Online (Sandbox Code Playgroud)

我试过以下代码:

tail -r parse.txt 
Run Code Online (Sandbox Code Playgroud)

unix bash shell

12
推荐指数
4
解决办法
2130
查看次数