我編寫了一個python腳本,它從列表中讀取文件偏移量和文件名,並將一個大文件劃分爲多個文件。爲了分裂,我使用了shell腳本,它將這些名稱和偏移量作爲輸入,並使用head命令創建多個輸出文件。我正在使用python將輸入發送到shell腳本。這在我的Windows 7和其他Linux系統中工作正常。但是,當我嘗試在ESX 6.5管理程序上使用相同的功能時,我意識到我無法在ESX 6.5中使用相同的shell腳本,因爲head命令不工作,因爲它在其他操作系統中工作。如何使用Python將單個文件分割成多個不同大小的文件
列表= [ 'IdleChk_1_E1.txt', '749', 'IdleChk_2_E1.txt', '749', 'reg_fifo_E1.txt', '5922', 'igu_fifo_E1.txt', '161',「protection_override_E1。 txt','1904','fw_asserts_E1.txt','708','McpTrace.txt','15578','phy_dump.txt','129','GrcDumpE1.bin','3629656']
偶數個元素是文件名,奇數個元素是大小。
這裏是我用來發送輸入shell腳本命令:
Process_three=subprocess.Popen("./read.sh %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s" \
%(''.join(map(str, list_info[1:2])), ''.join(map(str, list_info[0:1])),\
''.join(map(str, list_info[3:4])), ''.join(map(str, list_info[2:3])),\
''.join(map(str, list_info[5:6])), ''.join(map(str, list_info[4:5])),\
''.join(map(str, list_info[7:8])), ''.join(map(str, list_info[6:7])),\
''.join(map(str, list_info[9:10])), ''.join(map(str, list_info[8:9])),\
''.join(map(str, list_info[11:12])), ''.join(map(str, list_info[10:11])),\
''.join(map(str, list_info[13:14])), ''.join(map(str, list_info[12:13])),\
''.join(map(str, list_info[15:16])), ''.join(map(str, list_info[14:15])),\
''.join(map(str, list_info[17:18])), ''.join(map(str, list_info[16:17])),\
file_name), stdout=subprocess.PIPE, shell=True)
(temp, error) = Process_three.communicate()
這是我的shell腳本。
if [ "$#" -eq 19 ];
then
{
head -c $1 > $2
head -c $3 > $4
head -c $5 > $6
head -c $7 > $8
head -c $9 > ${10}
head -c ${11} > ${12}
head -c ${13} > ${14}
head -c ${15} > ${16}
head -c ${17} > ${18}
} < ${19}
fi
在ESX中,只有第一個head命令輸出正在工作。
是否有另一種分割文件的方法。我知道有分割命令,但這個命令將文件分成兩半。我需要動態大小文件。 我希望如果我可以從python本身進行拆分。順便說一句,我是Python的新手。
是否希望按行或塊大小拆分文件?文件文本還是二進制文件?或劑量它重要? –
我想分塊大小的文件。文件包含文本和二進制數據。 –
我推薦這個鏈接:https://www.safaribooksonline.com/library/view/programming-python-second/0596000855/ch04s02.html和這個鏈接在stackoverflow:http://stackoverflow.com/questions/8096614/split -large-files-using-python –