小编Jim*_*Jim的帖子

将bash数组传递给python列表

我正在尝试使用旧的getenv方法将数组从bash传递给python但是我一直收到此错误:

./crcFiles.sh: line 7: export: `0021': not a valid identifier
Traceback (most recent call last):
  File "/shares/web/vm3618/optiload/prog/legalLitres.py", line 30, in <module>
    for i in mdcArray.split(' '):
AttributeError: 'NoneType' object has no attribute 'split'
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么$ mdcNo没有成功从bash传递到python吗?

代码.sh:

#!/bin/bash

mdcNo=('0021' '0022' '0036' '0055' '0057' '0059' '0061' '0062' '0063' '0065' '0066' '0086' '0095' '0098' '0106' '0110' '0113' '0114' '0115' '0121' '0126' '0128' '0135' '0141' '0143' '0153' '0155' '0158')

localDIR=/shares/web/vm3618/optiload/prog

export mdcNo

$localDIR/legalLitres.py


for i in "${mdcNo[@]}"
do
echo $i
cp $localDIR/MDC$i/*/QqTrkRec.txt $localDIR/crccalc/.
cd …
Run Code Online (Sandbox Code Playgroud)

python linux arrays bash

9
推荐指数
2
解决办法
7680
查看次数

使用Paramiko将文件从一个目录移动到另一个目录

我有一个脚本,在SFTP服务器上创建和tmp目录,然后/tmp在传输完成后将文件放入,但是我需要将文件从/tmp一个目录移动到一个目录/.使用Paramiko如何将文件从一个远程目录移动到另一个目录?

步骤指南:

本地文件----->远程临时目录---->远程根目录

如果需要,可以使用以

#!/usr/bin/python

# --------------------------------------------------------------------
#import libraries
# --------------------------------------------------------------------
import paramiko as PM
import os
import datetime

# --------------------------------------------------------------------
# Global Variables
# --------------------------------------------------------------------

host = 'host IP address'
port = 22
username = 'Username'
password = '*********'

# Variable Paths

localPath = '/shares/MILKLINK/fromML'
remotePath = '/'
logPath = '/shares/MILKLINK/logs/PPcfg02.log'
SRCfiles = '/shares/MILKLINK/Milklink.cpy'

# --------------------------------------------------------------------
#  Create LOG FILE
# --------------------------------------------------------------------

log = open(logPath, 'a')
log.write(datetime.datetime.now().isoformat()+'\n')

# Creating lockfile

if(os.path.isfile('LockSFTP')):
    log.write("LOCK FILE …
Run Code Online (Sandbox Code Playgroud)

python linux ssh sftp paramiko

7
推荐指数
1
解决办法
6638
查看次数

保留Paramiko的时间戳

当使用Paramiko从一台服务器到另一台服务器的SFTP文件时,是否有一种方法可以保留时间戳,类似于-pLinux中的参数?

原始档案:

jim@vm3634:~$ ls -la
-rwxrwx---    1 jim  admin    2214 Mar 30 17:33 compcip.asc
Run Code Online (Sandbox Code Playgroud)

上传的文件:

sftp> ls -la
-rwxrwx---    1 no-user  no-group    2214 Mar 30 18:49 compcip.asc
Run Code Online (Sandbox Code Playgroud)

上传的文件需要与原始文件具有相同的时间戳.

python sftp paramiko filemtime

6
推荐指数
1
解决办法
1216
查看次数

ReportLab段落和文本格式

我的问题是,使用reportlab生成简单的文本文档时,它会丢失所有格式。我已经运行了几次尝试和调试它,问题似乎是当传递msgStrParagraph它时,它会丢失与它一起发送的所有格式。

有谁知道如何在保持当前文本格式的同时生成简单的pdf

码:

# PDF GENERATION LIBRARIES
# import the report lab PDF generation tools
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
Parts = []

def sumFile(msgStr = None, COMPLETE = 0):

    pdfmetrics.registerFont(TTFont('Inconsolata', 'Inconsolata-Regular.ttf'))

    summaryName = SimpleDocTemplate(vehID+".pdf")

    style = ParagraphStyle(
        name='Normal',
        fontName='Inconsolata',
        fontSize=8,
    )

    msgStr.replace('\n','<br />')

    if msgStr == "PageBreak":
        parts.append(PageBreak())
    else:
        parts.append(msgStr)

    if COMPLETE == …
Run Code Online (Sandbox Code Playgroud)

python pdf reportlab text-formatting

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

移动字典元素

我有一个未定义大小的字典(与用户想要的一样大)但我希望能够在需要时删除字典中的项目,我的问题是如何移除所有字典键一旦删除,即

transDict={0:"Charlie", 1:"Alan", 2:"Jake", 3:"Rose"}
delkey = raw_input('What key to delete')
transDict[delkey] = transDict.pop[int(delkey + 1)]

# this is the bit isn't working code just an 
# example of the functionality I need
for _id in transDict.keys(int(delkey) + 1:):
    transDict[_id] = transDict[(int(_id) + 1)]
Run Code Online (Sandbox Code Playgroud)

但后来我需要将所有其他元素从3开始向下移动一步,我的问题是字典可以是5或50个元素大.

谢谢

python

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

循环 sys.args

尝试循环时,sys.args出现以下错误:

Traceback (most recent call last):
  File "./autoCrosRef.py", line 59, in <module>
    cleanFile(inputArgs[i])
TypeError: list indices must be integers, not str
Run Code Online (Sandbox Code Playgroud)

这是我如何调用我的程序:

./autoCrosRef.py file.txt

这是我用来尝试循环的代码:

import sys
# ------
#  MAIN
# ------
inputArgs = sys.argv

print len(inputArgs)

for i in inputArgs[1:]:

    cleanFile(inputArgs[i])
Run Code Online (Sandbox Code Playgroud)

我的打印命令显示我在 cmd 中传递了 2 个参数,但它一直出错,是我调用它错误还是我的循环错误?

python linux for-loop command-line-arguments

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

循环遍历 tkinter 列表框

有没有办法循环遍历 tkinter 列表框中的项目,这样我就可以检查项目的第一部分,如果两个字符串匹配,则删除它并用新项目替换它?

例如,如果我有:

{'breakfast':['bacon','eggs','beans'],
 'lunch':['ham', 'cheese', 'bread'],
 'dinner':['Steak','Potato','Vegetables']}
Run Code Online (Sandbox Code Playgroud)

这使用字符串格式显示在列表框中:

breakfast: Bacon, Eggs, Beans
lunch: Ham, Cheese, Bread
dinner: Steak, Potato, Vegetables
Run Code Online (Sandbox Code Playgroud)

如果我更改breakfast['Cereal', 'Milk']如何更改列表框中的条目如果我不知道索引,并且只有字典键可以继续?

python user-interface listbox tkinter

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

编辑文件时保留时间戳

我想保留我在 for 循环中编辑的文件的时间戳

for files in $DIR/MDC*/$FILE
do
    # Need to get date here!
    dos2unix $files
    if grep -q $TYPE $files; then
        echo 'done'
    else
        sed -i "1s/^/$TYPE\n/" $files
    fi
    $DIR/crccalc/LDDR16.py $files
    # Use Date variable here to change it back

done
Run Code Online (Sandbox Code Playgroud)

问题是我需要从文件中获取格式化的日期字符串,以便touch -r在循环完成后恢复文件日期。

stat 没有给我我需要的格式。

所需格式:

YYMMDDhhmm

linux bash shell date string-formatting

0
推荐指数
1
解决办法
1150
查看次数