PHP实现将多个文件中的内容合并为新文件的方法示例,PHP实现将多个文件中的内容合并为新文件的方法

正文实例呈报了PHP实现将多少个公文中的内容统一为新文件的形式。分享给大家供大家参照他事他说加以考察,具体如下:

正文实例汇报了PHP达成将四个文件中的内容统一为新文件的章程。分享给大家供大家仿照效法,具体如下:

正文实例呈报了PHP完成的大文件切割与统一功用。分享给大家供大家参照他事他说加以考察,具体如下:

本文实例汇报了Python实现分割文件及联合文件的秘技。分享给我们供大家参考。具体如下:

 

function test()
{
  $hostdir= iconv("utf-8","gbk","C:\Users\原万里\Desktop\日常笔记") ; //iconv()转换编码方式,将UTF-8转换为gbk,若是报错在gbk后加//IGNORE
  $filesnames = scandir($hostdir); //scandir() 函数返回指定目录中的文件和目录的数组。默认升序排列,
  foreach ($filesnames as $name) {
       if($name!=".." && $name!=".") //遍历结果中会多出‘.'以及‘..',没有用处,不予处理;
       {
         $cipath = $hostdir.$name;
         $cjfilenames = scandir($cipath); //根据情况再决定是否再向下遍历一次
         foreach($cjfilenames as $cjname)
         {
            if($cjname!=".." && $cjname!="." &&!is_dir($cipath."/".$cjname))
             {
               $str = file_get_contents($cipath."/".$cjname);
               file_put_contents("e:/test.txt",$str,FILE_APPEND);
             }
         }
       }
     echo "\n";
  }
}

分开代码

分割文件split.py如下:

 代码如下

PHP实现将多个文件中的内容合并为新文件的方法示例,PHP实现将多个文件中的内容合并为新文件的方法。更加多关于PHP相关内容感兴趣的读者可查看本站专项论题:《PHP目录操作本领汇总》、《php文件操作总计》、《PHP常用遍历算法与能力总计》、《PHP数据结构与算法教程》、《php程序设计算法计算》、《PHP数组(Array)操作本事大全》、《php字符串(string)用法计算》及《php常见数据库操作本领汇总》

split.php

#!/usr/bin/python
##########################################################################
# split a file into a set of parts; join.py puts them back together;
# this is a customizable version of the standard unix split command-line 
# utility; because it is written in Python, it also works on Windows and
# can be easily modified; because it exports a function, its logic can 
# also be imported and reused in other applications;
##########################################################################
import sys, os
kilobytes = 1024
megabytes = kilobytes * 1000
chunksize = int(1.4 * megabytes)     # default: roughly a floppy
def split(fromfile, todir, chunksize=chunksize): 
 if not os.path.exists(todir):     # caller handles errors
  os.mkdir(todir)       # make dir, read/write parts
 else:
  for fname in os.listdir(todir):   # delete any existing files
   os.remove(os.path.join(todir, fname)) 
 partnum = 0
 input = open(fromfile, 'rb')     # use binary mode on Windows
 while 1:          # eof=empty string from read
  chunk = input.read(chunksize)    # get next part <= chunksize
  if not chunk: break
  partnum = partnum+1
  filename = os.path.join(todir, ('part%04d' % partnum))
  fileobj = open(filename, 'wb')
  fileobj.write(chunk)
  fileobj.close()       # or simply open().write()
 input.close()
 assert partnum <= 9999       # join sort fails if 5 digits
 return partnum
if __name__ == '__main__':
 if len(sys.argv) == 2 and sys.argv[1] == '-help':
  print 'Use: split.py [file-to-split target-dir [chunksize]]'
 else:
  if len(sys.argv) < 3:
   interactive = 1
   fromfile = raw_input('File to be split? ')  # input if clicked 
   todir = raw_input('Directory to store part files? ')
  else:
   interactive = 0
   fromfile, todir = sys.argv[1:3]     # args in cmdline
   if len(sys.argv) == 4: chunksize = int(sys.argv[3])
  absfrom, absto = map(os.path.abspath, [fromfile, todir])
  print 'Splitting', absfrom, 'to', absto, 'by', chunksize
  try:
   parts = split(fromfile, todir, chunksize)
  except:
   print 'Error during split:'
   print sys.exc_info()[0], sys.exc_info()[1]
  else:
   print 'Split finished:', parts, 'parts are in', absto
  if interactive: raw_input('Press Enter key') # pause if clicked

functiontest()

企望本文所述对我们PHP程序设计有着协理。

<?php
$i  = 0;                 //分割的块编号
$fp  = fopen("hadoop.sql","rb");      //要分割的文件
$file = fopen("split_hash.txt","a");    //记录分割的信息的文本文件,实际生产环境存在redis更合适
while(!feof($fp)){
    $handle = fopen("hadoop.{$i}.sql","wb");
    fwrite($handle,fread($fp,5242880));//切割的块大小 5m
    fwrite($file,"hadoop.{$i}.sql\r\n");
    fclose($handle);
    unset($handle);
    $i++;
}
fclose ($fp);
fclose ($file);
echo "ok";

统一文件join_file.py如下:

{

你只怕感兴趣的小说:

  • PHP搭建大文件切割分块上传成效示例
  • PHP
    读取和修改大文件的某行内容的代码
  • 用PHP读取超大文件的实例代码
  • PHP读取大文件末尾N行的登时方法推荐
  • php超快高效能总括大文件行数
  • PHP读取大文件的二种艺术介绍
  • PHP大文件分割上传
    PHP分片上传
  • php使用file函数、fseek函数读取大文件效用比较分析
  • PHP
    读取大文件并出示的简便实例(推荐)
  • php断点续传之怎样分割合併文件
  • PHP合併静态文件详解
  • PHP达成的大文件切割与统一效用示例

联合代码

#!/usr/bin/python
##########################################################################
# join all part files in a dir created by split.py, to recreate file. 
# This is roughly like a 'cat fromdir/* > tofile' command on unix, but is 
# more portable and configurable, and exports the join operation as a 
# reusable function. Relies on sort order of file names: must be same 
# length. Could extend split/join to popup Tkinter file selectors.
##########################################################################
import os, sys
readsize = 1024
def join(fromdir, tofile):
 output = open(tofile, 'wb')
 parts = os.listdir(fromdir)
 parts.sort()
 for filename in parts:
  filepath = os.path.join(fromdir, filename)
  fileobj = open(filepath, 'rb')
  while 1:
   filebytes = fileobj.read(readsize)
   if not filebytes: break
   output.write(filebytes)
  fileobj.close()
 output.close()
if __name__ == '__main__':
 if len(sys.argv) == 2 and sys.argv[1] == '-help':
  print 'Use: join.py [from-dir-name to-file-name]'
 else:
  if len(sys.argv) != 3:
   interactive = 1
   fromdir = raw_input('Directory containing part files? ')
   tofile = raw_input('Name of file to be recreated? ')
  else:
   interactive = 0
   fromdir, tofile = sys.argv[1:]
  absfrom, absto = map(os.path.abspath, [fromdir, tofile])
  print 'Joining', absfrom, 'to make', absto
  try:
   join(fromdir, tofile)
  except:
   print 'Error joining files:'
   print sys.exc_info()[0], sys.exc_info()[1]
  else:
   print 'Join complete: see', absto
  if interactive: raw_input('Press Enter key') # pause if clicked

  $hostdir= iconv(“utf-8″,”gbk”,”C:\Users\原万里\Desktop\普普通通笔记”)
;//iconv()调换编码形式,将UTF-8转换为gbk,倘诺报错在gbk后加//IGNORE

merge.php

瞩望本文所述对大家的Python程序设计有所帮助。

  $filesnames= scandir($hostdir);//scandir()
函数再次回到钦命目录中的文件和目录的数组。私下认可升序排列,

<?php
$hash = file_get_contents("split_hash.txt"); //读取分割文件的信息
$list = explode("\r\n",$hash);
$fp = fopen("hadoop2.sql","ab");    //合并后的文件名
foreach($list as $value){
  if(!empty($value)) {
    $handle = fopen($value,"rb");
    fwrite($fp,fread($handle,filesize($value)));
    fclose($handle);
    unset($handle);
  }
}
fclose($fp);
echo "ok";

您只怕感兴趣的稿子:

  • python读取LMDB中图像的方法
  • python读写LMDB文件的办法
  • python将多个公文文件合併为三个文件的代码(便于搜索)
  • Python实现将引得中TXT合并成一个大TXT文件的不二等秘书技
  • python完毕公文文件合并
  • python合併文本文件示例
  • python中联合五个文本文件并依据姓名首字母排序的例证
  • python 合并文件的切切实实实例
  • Python将三个excel文件合併为二个文书
  • 如何用Python合并lmdb文件

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图